Attaching Files to Organisation (via API)
Hi,
We just got onto Insightly and are transferring some data. Since the other system is being used, to do this by scripts that can be run time and again to get the latest status into Insightly is more appropriate than trying to to this with csv-files or the like.
Now struggling a lot with attaching files to organizations.
Using the API 2.2 as described here: https://api.insight.ly/v2.2/Help#!/Organisations/AddFileAttachment and trying to implement it in PHP
Posting the request to a requestbin, I get in the body:
--------------------------04e0324f8d928804 Content-Disposition: form-data; name="id" 122447356 --------------------------04e0324f8d928804 Content-Disposition: form-data; name="file" <encoded imagedata>.... --------------------------04e0324f8d928804 Content-Disposition: form-data; name="file_name" Novagear_Logo_RGB.jpg --------------------------04e0324f8d928804 Content-Disposition: form-data; name="content_type" image/jpeg --------------------------04e0324f8d928804 Content-Disposition: form-data; name="file_category_id" 5798273 --------------------------04e0324f8d928804--
The header contains:
Content-Type: multipart/form-data; boundary=------------------------04e0324f8d928804
If I post the same thing to the endpoint:
/Organisations/{id}/FileAttachments with {id} being replaced by the proper organization id (122447356 in the case above), I get a 400 response.
How exactly should the body look like?
from the other API, I get the imagedata as raw data. I (Insightly, to be more precise) cannot access the according URI to that image
Comments
Hi Matthias,
Request header Content-Type should be "multipart/form-data".
As the request body, you should provide correct content_type parameter like "image/jpeg", file_name parameter like "newImage.jpg" and you just need to encode your file as a multipart/form-data and send that value as file parameter in order to save it.
Hope that helps.
Thanks,
Oguzhan
Just an addition:
I tried to manually construct the string, wrt to the python API library.
My body now looks like this:
And ok, I found the mistake. Manually constructing the body, the boundary field must be manually included in the Content-Type field.
For others, in PHP:
- the "automatic" multipart/form-data body generation with CURL by passing an array to CURLOPT_POSTFIELDS did not work for me or I could not figure out, how to add within the multipart boundaries, the Content-Type Field or any other variable but 'name' in the Content-Disposition Field (see my first post)
- one can construct the whole body manually, as a string. However, if this is done, the boundary variable must be added to the Content-Type field (Content-Type: multipart/form-data; boundary=.....). For the automatic version above, this is done by the CURL package automatically, but not when a string instead of an array is specified
Please sign in to leave a comment.