How to do a PUT request with cURL?

RestHttpCurl

Rest Problem Overview


How do I test a RESTful PUT (or DELETE) method using cURL?

Rest Solutions


Solution 1 - Rest

Using the -X flag with whatever HTTP verb you want:

curl -X PUT -d arg=val -d arg2=val2 localhost:8080

This example also uses the -d flag to provide arguments with your PUT request.

Solution 2 - Rest

Quick Answer:

In a single line, the curl command would be:

  1. If sending form data:

    curl -X PUT -H "Content-Type: multipart/form-data;" -F "key1=val1" "YOUR_URI"
    
  2. If sending raw data as json:

    curl -X PUT -H "Content-Type: application/json" -d '{"key1":"value"}' "YOUR_URI"
    
  3. 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.

POSTMAN to get the request code

Solution 3 - Rest

An example PUT following Martin C. Martin's comment:

curl -T filename.txt http://www.example.com/dir/

With -T (same as --upload-file) curl will use PUT for HTTP.

Solution 4 - Rest

curl -X PUT -d 'new_value' URL_PATH/key

where,

X - option to be used for request command

d - option to be used in order to put data on remote url

URL_PATH - remote url

new_value - value which we want to put to the server's key

Solution 5 - Rest

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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnView Question on Stackoverflow
Solution 1 - ResttheabrahamView Answer on Stackoverflow
Solution 2 - RestPrateekView Answer on Stackoverflow
Solution 3 - RestTor KlingbergView Answer on Stackoverflow
Solution 4 - Restkalyani chaudhariView Answer on Stackoverflow
Solution 5 - RestsunitkatkarView Answer on Stackoverflow