How do I test a RESTful PUT (or DELETE) method using cURL?
5 Answers
632
votes
147
votes
Quick Answer:
In a single line, the curl command would be:
a) If sending form data:
curl -X PUT -H "Content-Type: multipart/form-data;" -F "key1=val1" "YOUR_URI"
b) If sending raw data as json:
curl -X PUT -H "Content-Type: application/json" -d '{"key1":"value"}' "YOUR_URI"
c) If sending a file with a POST request:
curl -X POST "YOUR_URI" -F 'file=@/file-path.csv'
Alternative solution:
You can use the POSTMAN app from Chrome Store to get the equivalent cURL request. This is especially useful when writing more complicated requests.
For the request with other formats or for different clients like java, PHP, you can check out POSTMAN/comment below.
33
votes
8
votes
6
votes
I am late to this thread, but I too had a similar requirement. Since my script was constructing the request for curl dynamically, I wanted a similar structure of the command across GET, POST and PUT.
Here is what works for me
For PUT request:
curl --request PUT --url http://localhost:8080/put --header 'content-type: application/x-www-form-urlencoded' --data 'bar=baz&foo=foo1'
For POST request:
curl --request POST --url http://localhost:8080/post --header 'content-type: application/x-www-form-urlencoded' --data 'bar=baz&foo=foo1'
For GET request:
curl --request GET --url 'http://localhost:8080/get?foo=bar&foz=baz'