How to post a file and form data using curl

There will be times where you need to test an API file upload or just play around with sending data. curl supports this very well by making a Content-Type: multipart/form-data request along with you can send data and also the contents of a file.

curl is used in command lines or scripts to transfer data. It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands of software applications affecting billions of humans daily.

curl -X POST \
    -F "param1=value1" \
    -F "param2=value2" \
    -F "the_name_of_the_file=@path/to/file/test_results.xml" \
    localhost:3000 # the url you will be making the post to

This creates a request that contains the contents of the file.

╰ nc -l 3000 & curl -X POST -F "the_name_of_the_file=@test_results.xml" -F "param1=value1" -F "param2=value2" localhost:3000

[1] 15261
Host: localhost:3000
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 34820
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------3e33d10af5891a16

Content-Disposition: form-data; name="report"; filename="coverage.xml"
Content-Type: application/xml

<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1515737431">
Content-Disposition: form-data; name="param1"

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


Show Comments

Get the latest posts delivered right to your inbox.