Upload file to Circuit Conversation via REST API - Backend Upload

84 Views Asked by At

I am unable to upload a file with the file API

I've tried this HTTP request in POSTMAN and Curl with no success and the same result on both: Attach a file (picture) to a conversation

Would you be able to share a real working example from Postman, or from Postman convert to a Curl code snippet that I can import?

The following returns "wrong Content-Disposition header was set"

POST /rest/v2/fileapi HTTP/1.1
Host: circuitsandbox.net
Authorization:  Bearer MyTokenCodeGoesHere
Content-Length:  100
Content-Disposition:  attachment; filename="test.txt"
Cache-Control:  no-cache

MyBinaryCodeGoesHere

The above looks like this in curl:

curl --location --request POST "https://circuitsandbox.net/rest/v2/fileapi" \
    --header "Authorization:  Bearer MyTokenCodeGoesHere" \
    --header "Content-Length:  100" \
    --header "Content-Disposition:  attachment; filename=\"test.txt\"" \
    --header "Cache-Control:  no-cache" \
    --header "MyBinaryCodeGoesHere: "

Tested with Host: local.circuit.com instead of Host: circuitsandbox.net, no connection, I assumed it was just an example, but mentioning just in case.

Expected:

{"fileId":"fb211fd6-df53-4b82-824d-986dac47b3e7","attachmentId":"ZmIyMT..."}

Actual result:

"wrong Content-Disposition header was set"

2

There are 2 best solutions below

1
On BEST ANSWER

Here is a curl example posting a json document:

curl -X POST https://circuitsandbox.net/rest/v2/fileapi \
 -H 'authorization: Bearer <token>' \
 -H 'cache-control: no-cache' \
 -H 'content-disposition: attachment; filename="test.json"' \
 -H "Content-Type: application/json" \
 -d '{"key":"val"}'

Using postman you can easily set a file to upload in the body's binary tab. The only headers you'll need are "Authorization" and "Content-Disposition". The "Content-Disposition" header has the format: attachment; filename="test.log"

In your example the data does not look right. It should not be passed in a header.

0
On

Just to add up for anyone who might find this useful.

Here is my HTTP code:

POST /rest/v2/fileapi HTTP/1.1
Host: circuitsandbox.net
Authorization:  Bearer <token>
Cache-Control:  no-cache
Content-Disposition:  attachment; filename="test.txt"
Content-Type: text/plain

{"src":"/C:/Temp/test.txt"}

Here is my Curl code:

curl --location --request POST "https://circuitsandbox.net/rest/v2/fileapi" \
    --header "Authorization:  Bearer <token>" \
    --header "Cache-Control:  no-cache" \
    --header "Content-Disposition:  attachment; filename=\"test.txt\"" \
    --header "Content-Type: text/plain" \
    --data-binary "@C:\Temp\test.txt"

For anyone reading this, here is a list of MIME types, your content-type changes depending on the type of document you want to upload:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types

More on -d (--data-binary): https://bagder.gitbooks.io/everything-curl/http-post.html