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

LinuxJsonCommand LineWeb Crawler

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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

Solution 3 - Linux

You could use wget with post-file as well, which I found useful.

wget --post-file=[file] --header=Content-Type:application/json [URL]

You can keep the content in the file and the content will be sent as post data.

Solution 4 - Linux

curl --request POST \
--url http://localhost:8099/someservice/services/boo \
--header 'authorization: Basic dkfhsdlepwmdseA==' \
--header 'cache-control: no-cache' \
--header 'content-type: application/json' \
--data '{"value": "24.127.1212.123"}'

Solution 5 - Linux

Have you looked at curl? It's very good at facilitating HTTP GET/POST requests via the command line.

e.g. (for a GET request):

C:\WINDOWS>curl "http://search.twitter.com/search.json?q=twitterapi&result_type=
popular"
{"results":[{"from_user_id_str":"32316068","profile_image_url":"http://a2.twimg.
com/profile_images/351010682/twitblock_profile_normal.png","created_at":"Thu, 25
 Nov 2010 14:37:46 +0000","from_user":"twitblockapp","id_str":"7805146834669569"
,"metadata":{"result_type":"popular","recent_retweets":10},"to_user_id":null,"te
xt":"blocking and reporting functions are currently failing. @TwitterAPI have be
en notified. http://j.mp/id5w3m","id":7805146834669569,"from_user_id":32316068,"
geo":null,"iso_language_code":"en","to_user_id_str":null,"source":"<a href=&q
uot;http://twitter.com" rel="nofollow">Tweetie for Mac</a&g
t;"}],"max_id":9607558079713280,"since_id":0,"refresh_url":"?since_id=9607558079
713280&q=twitterapi","results_per_page":15,"page":1,"completed_in":0.012698,"sin
ce_id_str":"0","max_id_str":"9607558079713280","query":"twitterapi"}

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
QuestionRoaltView Question on Stackoverflow
Solution 1 - LinuxPius RaederView Answer on Stackoverflow
Solution 2 - LinuxnosView Answer on Stackoverflow
Solution 3 - LinuxRangaView Answer on Stackoverflow
Solution 4 - LinuxNeelsView Answer on Stackoverflow
Solution 5 - LinuxJason SView Answer on Stackoverflow