Linux 如何从命令行使用 JSON 有效负载进行 HTTP 请求/调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4315111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do HTTP-request/call with JSON payload from command-line?
提问by Roalt
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.
从命令行进行 JSON 调用的最简单方法是什么?我有一个网站,它执行 JSON 调用来检索其他数据。
The Request Payloadas shown in Google Chrome looks like this:
Google Chrome 中显示的请求有效负载如下所示:
{"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 命令行进行调用并检索 JSON 内容,而不是解析传入的 JSON 数据。
采纳答案by nos
Use curl, assuming the data is POST'ed, something like
使用 curl,假设数据已发布,类似于
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
如果您只是使用 GET 检索数据,并且不需要发送任何栏 URL 参数,则只需运行 curl http://example.com/some/path
回答by Jason S
Have you looked at curl? It's very good at facilitating HTTP GET/POST requests via the command line.
你看过curl吗?它非常擅长通过命令行促进 HTTP GET/POST 请求。
e.g. (for a GET request):
例如(对于 GET 请求):
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"}
回答by Pius Raeder
You could use wget as well:
你也可以使用 wget :
wget -O- --post-data='{"some data to post..."}' \
--header='Content-Type:application/json' \
'http://www.example.com:9000/json'
回答by Neels
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"}'
回答by Ranga
You could use wget
with post-file
as well, which I found useful.
你也可以使用wget
with post-file
,我发现它很有用。
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.
您可以将内容保留在文件中,内容将作为post
数据发送。