Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using curl for multipart/form-data with a file upload

Tags:

curl

multipart

I have a server API that I have developed against the format used by Fiddler to do HTTP posts of files, which is a multipart/form-data post. I'm trying to get curl to do something similar (so I can stop using Fiddler for testing and instead have a separate program call out to curl programmatically).

Here is how it's set up in Fiddler, which is what I need to replicate. The file being uploaded is myfile.html:

---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="myfile.html"
Content-Type: text/html

<@INCLUDE *C:\myfiles\myfile.html*@>
---------------------------acebdf13572468--

So what I need is for curl to produce something similar: in particular the name= part, followed by the file content at the bottom of this part. I've tried this with:

curl -F "fieldNameHere=myfile.html" http://myapi.com/

When I do that it seems to totally ignore my file though. Here's the verbose output if I add -v:

POST / HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: perl-h4.factset.io
Accept: */*
Content-Length: 166
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------981301fdaeb3

There's no file content at all (it's a large HTML file). So I think there might be something fundamental I'm missing here. Any pointers would be quite welcome.

Also, I have checked other StackOverflow questions like this: What is the right way to POST multipart/form-data using curl?

But they are basically saying to do something like what I'm doing here. So maybe the problem is not my syntax, but some other reason it doesn't want to read the file.

like image 216
Stephen Avatar asked Oct 26 '17 17:10

Stephen


People also ask

How do you send a multipart file in curl?

With curl, you add each separate multipart with one -F (or --form ) flag and you then continue and add one -F for every input field in the form that you want to send. The above small example form has two parts, one named 'person' that is a plain text field and one named 'secret' that is a file.

How do you send a file using multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

Can we upload a file using curl?

If you want to upload data using the bulk API, you can use a cURL request to upload data from a . json or . csv file located on your machine.

How do I use curl to transfer files?

Send the data using curl's –d or –data option. To execute a CURL file upload, you need to use the –d command-line option and begin data with the @ symbol. The file's name should come after the data with @ symbol so CURL can read it and send it to the server.


2 Answers

curl -X POST  -F [email protected]  http://myapi.com/

or

-X POST is implied by -F (per comment), quotes are optional

curl -F "[email protected]"  http://myapi.com/
like image 178
Scriptonomy Avatar answered Oct 08 '22 15:10

Scriptonomy


For more complex requests:

curl
 -X POST "YOUR_API_URL"
 -H "Authorization: Bearer YOUR_TOKEN"
 -H "Accept: application/json"
 -H "Content-Type: multipart/form-data"
 -F "meta={\"YOUR_ATTRIBUTE\": \"YOUR_DATA\"};type=application/json"
 -F "text=PATH_TO_FILE;type=text/plain"

You can set several chunks together into one request. For more, check out: https://learn.microsoft.com/en-us/azure/digital-twins/how-to-add-blobs!

like image 42
Adam Gerard Avatar answered Oct 08 '22 14:10

Adam Gerard