Attaching Files to Organisation (via API)


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:!/Organisations/AddFileAttachment and trying to implement it in PHP

Posting the request to a requestbin, I get in the body:

Content-Disposition: form-data; name="id"

Content-Disposition: form-data; name="file"

<encoded imagedata>....
Content-Disposition: form-data; name="file_name"

Content-Disposition: form-data; name="content_type"

Content-Disposition: form-data; name="file_category_id"


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



  • Official comment

    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.


    Comment actions Permalink
  • Just an addition:

    I tried to manually construct the string, wrt to the python API library.

    My body now looks like this:

    Content-Disposition: form-data; name="Novagear_Logo_RGB.jpgxxxxxx314"; filename="Novagear_Logo_RGB.jpg"
    Content-Type: image/jpeg
    <all the file data>....

    I also added a "Content-Length" header. Still I keep getting a 400 error, also with this solution

    Comment actions Permalink
  • 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

    Comment actions Permalink

Please sign in to leave a comment.