68

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.

Roalt
  • 8,330
  • 7
  • 41
  • 53

5 Answers5

100

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.

Pius Raeder
  • 1,423
  • 1
  • 14
  • 20
  • 5
    What is `-O-`? Is this a kebab? ) – Slava Fomin II Jan 18 '22 at 07:14
  • From the man page `-O file` is the shorthand option for `--output-document=file` and the dash (`-`) usually represents the file descriptor for standard out of the current tty. It just means "print the result to the console instead of writing it to a file". – Pius Raeder Jan 19 '22 at 08:40
  • 2
    Thanks. My point is that this argument looks cryptic, I would suggest to add this explanation to the answer ;) – Slava Fomin II Jan 21 '22 at 15:06
  • 1
    @SlavaFominII thanks for pointing that out. I've updated the answer to contain an explanation about the `-O` option. – Pius Raeder Jan 26 '22 at 07:31
57

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

nos
  • 223,662
  • 58
  • 417
  • 506
13

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.

Ali
  • 2,702
  • 3
  • 32
  • 54
Ranga
  • 1,191
  • 12
  • 19
8
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"}'
Neels
  • 184
  • 1
  • 6
-2

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"}
Jason S
  • 184,598
  • 164
  • 608
  • 970