Skip to content

Instantly share code, notes, and snippets.

@z11i
Last active April 18, 2024 13:18
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z11i/fdd874573fc4956c3ee3aad164eddbfe to your computer and use it in GitHub Desktop.
Save z11i/fdd874573fc4956c3ee3aad164eddbfe to your computer and use it in GitHub Desktop.
Use curl to upload a file in a multipart/form-data request, with custom content-type for the file (not the request)
filename='yourfilename'
filetype='text/csv'
token='my oauth token'
url='http://localhost/upload'
curl "$url" \
--form "data=@$filename;type=$filetype" \
--form "name=somename" \
-H "Authorization: Bearer $token"
@z11i
Copy link
Author

z11i commented Dec 2, 2020

This generates the below HTTP request:

POST /upload HTTP/1.1

Host: localhost
User-Agent: curl/7.73.0
Accept: */*
Authorization: Bearer my oauth token
Content-Length: 13087
Content-Type: multipart/form-data; boundary=------------------------9aa33de891382f2a

--------------------------9aa33de891382f2a
Content-Disposition: form-data; name="name"

somename
--------------------------9aa33de891382f2a
Content-Disposition: form-data; name="data"; filename="yourfilename"
Content-Type: text/csv

...your file content...

Note that there's another Content-Type in the multipart request's body. It's different from the multipart request itself. This Content-Type is specified using the type= part in the --form header. If you didn't specify this, a default content type would be application/octet-stream (i.e. a binary file).

The curl command generated from Postman is almost always wrong, as Postman relies on Chromium to do the multipart handling.

@mitchtchesnitch
Copy link

Thank you so much for this! Scoured the entire internet until I found a simple explanation as to why all my requests were denied.

You sir, are a gentleman and a scholar, thanks!

@lpicquet
Copy link

thanks, that saved me quite the headache!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment