How to upload JSON using curl http PUT request ?

[bash] USER_NAME=myusername PASSWD=mypassword SERVER_PUT_URL=https://myserver.url curl -k \ -H "Accept: application/json" \ -H "Content-Type:application/json" \ -X PUT -u $USER_NAME:$PASSWD \ -d ‘{"name":"myname","$email":"[email protected]","active":true}’ "$SERVER_PUT_URL" [/bash]

When we are uploading any JSON using curl http POST, we have to mention the same i.e. JSON in curl header with following two arguments,

-H "Accept: application/json" \
-H "Content-Type:application/json" \

The next argument we pass to curl is “-X” i.e. http request type, since we need to upload something, we have used http “PUT” as request type.

If uploading has been protected with authentication using username & password, we can pass the same using “-u $USER_NAME:$PASSWD” as command line parameters for user authentication.

The next argument is “-d” i.e. data which contains the actual JSON which we need to upload to the server.

The last argument is the SERVER URL where we are uploading the JSON.

If there is any return string from this http PUT command, we can accept this as below,

[bash] PUT_RET_STRING=$(curl -k \ -H "Accept: application/json" \ -H "Content-Type:application/json" \ -X PUT -u $USER_NAME:$PASSWD \ -d ‘{"name":"myname","$email":"[email protected]","active":true}’ "$SERVER_PUT_URL") echo "http put returned string as : $PUT_RET_STRING" [/bash]

Leave a Comment