Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do HTTP-request/call with JSON payload from command-line?

What's the easiest way to do a JSON call from the command-line? I have a website that does a JSON call to retrieve additional data.

The Request Payload as shown in Google Chrome looks like this:

{"version": "1.1", "method":"progr","id":2,"params":{"call":...} } 

It's about doing the call from (preferably) linux command line and retrieving the JSON content, not about parsing the incoming JSON data.

like image 431
Roalt Avatar asked Nov 30 '10 15:11

Roalt


People also ask

Can we pass JSON payload GET request?

To answer your question, yes you may pass JSON in the URI as part of a GET request (provided you URL-encode).

How do you pass payload in curl command?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.

What is request payload in JSON?

A request payload is data that clients send to the server in the body of an HTTP POST, PUT, or PATCH message that contains important information about the request.


2 Answers

You could use wget as well:

wget -O- --post-data='{"some data to post..."}' \   --header='Content-Type:application/json' \   'http://www.example.com:9000/json' 

Calling wget with the option -O providing the - (space in between will be ignored, so it could also be written as -O -) to it as its value will cause wget to output the HTTP response directly to standard output instead into a file. The long option name for that is --output-document=file.

like image 170
Pius Raeder Avatar answered Sep 23 '22 21:09

Pius Raeder


Use curl, assuming the data is POST'ed, something like

curl -X POST http://example.com/some/path -d '{"version": "1.1", "method":"progr","id":2,"params":{"call":...} }' 

If you're just retrieving the data with a GET , and don't need to send anything bar URL parameters, you'd just run curl http://example.com/some/path

like image 22
nos Avatar answered Sep 19 '22 21:09

nos