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.

Related :   Starting webserver using your first simple Django project

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" 

lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


Leave a Comment

Android Android Build system Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded