Help with API "add file"

27380

I've got an application that is based on PHP 7.0 and that uses the API to collaborate.

The basic API connections work, I can read directories and create folders.

For example folder creation:

 

<?PHP$accesstoken='123';$xml='<?xml version="1.0" encoding="UTF-8" standalone="yes"?><folder><name>Test HH2</name><description>Via API</description><accessInherited>1</accessInherited></folder>';$ch = curl_init();curl_setopt($ch, CURLOPT_URL,"instancename.highq.com/.../folders  curl_setopt($ch, CURLOPT_PROXY, "456");curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml; charset=utf-8','Authorization:Bearer '.$accesstoken));curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS,$xml);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$server_output = curl_exec($ch);curl_close ($ch);echo $server_output;?>

 
But I can not manage the file upload.
 
I follow the API instruction here:
collaborate.highq.com/.../wikiHome.action

My code looks like this:

 

<?PHP$accesstoken='123';$file=file_get_contents('/path/testhh.txt');$fields = array('file' => $file, 'filename' => 'test2.txt');$ch = curl_init();curl_setopt($ch, CURLOPT_URL,"instancename.highq.com/.../content curl_setopt($ch, CURLOPT_PROXY, "456");curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data','Authorization:Bearer '.$accesstoken));curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS , $fields);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$server_output = curl_exec($ch);curl_close ($ch);echo($server_output);?>

 

The file “test2.txt” mentioned here exists, so that’s not the problem.  

But the resulting error is:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><error><link>instancename.highq.com/.../ref><summary>Access denied</summary></error>

 
Could someone help me here?
 
Thx, Helmut

  • Hi Helmut (Efs Rinda), can you post the HTTP headers being sent? (with sensitive values redacted)

    I personally don't use PHP, so I'd be interested to know what is setting the boundary value of your multi-part form? i.e. in a normal POST request I would expect to see: Content-Type: multipart/form-data; boundary=----someGeneratedBoundaryId, where that boundary Id is used to separate your filename and file form values.

    Also, it's worth checking that the Content-Length header is included (see wiki for why)

  • Thx Andrew,

    I've already tried different boundary strategies with no success. And since boundaries are not mentioned in the API description I digged no further.

    Now I found a different solutions. In the above code I replaced "file_get_contents" with "curl_file_create" and now it works. (Maybe this function creates boundaries too.)

    The Content-Length will be something I will look at later, because I pass the data as an array and arrays have no length. Until now I had no troubles without Content-Length.

  • Hi Helmut, I would assume it does. From what I read, isn't that part of CURLFile, the preferred method, more so in 7. Like I said I don't touch PHP. You have to set boundaries if you are sending multiple form values. Only if you're sending just file contents can you get away with it, even then it's up to the server what it enforces. As a side, and for anyone experiencing this, if when you upload your file, the server "stores" it as an application/octet-stream, it's because you need to include the mime type in the multi-part form, as well as filename, and file data.