Home » Cloud Technologies » How to upload JSON using curl http PUT request ?

How to upload JSON using curl http PUT request ?

For uploading JSON using curl http PUT request, we have written a simple script as below. You can also type the command on terminal by replacing with correct definitions as done at the beginning of the script.

#!/bin/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":"hello@world.com","active":true}' "$SERVER_PUT_URL"

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,

PUT_RET_STRING=$(curl -k \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X PUT -u $USER_NAME:$PASSWD \
-d '{"name":"myname","$email":"hello@world.com","active":true}' "$SERVER_PUT_URL")

echo "http put returned string as : $PUT_RET_STRING" 

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment