使用 json 参数卷曲 GET 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21326397/
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
Curl GET request with json parameter
提问by Pradeep Simha
I am trying to send a "GET" request to a remote REST API from Command Prompt via cURL like this:
我正在尝试通过 cURL 从命令提示符向远程 REST API 发送“GET”请求,如下所示:
curl -X GET -H "Content-type: application/json" -H "Accept: application/json" "http://server:5050/a/c/getName/{"param0":"pradeep"}"
But it returns no output. I tried to ping the URL directly from the browser, I am able to get response successfully, I don't understand what is the wrong with the command.
但它不返回任何输出。我尝试直接从浏览器ping URL,我能够成功获得响应,我不明白该命令有什么问题。
Basically I want to set a "GET" request to a remote REST service which gives me json data as a response via curl. Can anyone guide me what mistake am I doing? I tried various posts, but all of them talk about POST requests not about GET.
基本上,我想向远程 REST 服务设置一个“GET”请求,该服务通过 curl 为我提供 json 数据作为响应。谁能指导我我在做什么错误?我尝试了各种帖子,但所有帖子都在谈论 POST 请求而不是 GET。
回答by Harshal Bulsara
This should work :
这应该工作:
curl -i -H "Accept: application/json" 'server:5050/a/c/getName{"param0":"pradeep"}'
use option -i instead of x.
使用选项 -i 而不是 x。
回答by Martin Seeler
If you want to send your data inside the body, then you have to make a POSTor PUTinstead of GET.
如果你想在 body 内发送你的数据,那么你必须使用POSTorPUT代替GET.
For me, it looks like you're trying to send the query with uri parameters, which is not related to GET, you can also put these parameters on POST, PUTand so on.
对我来说,看起来您正在尝试使用 uri 参数发送查询,这与 无关GET,您也可以将这些参数放在 上POST,PUT依此类推。
The query is an optional part, separated by a question mark ("?"), that contains additional identification information that is not hierarchical in nature. The query string syntax is not generically defined, but it is commonly organized as a sequence of = pairs, with the pairs separated by a semicolon or an ampersand.
查询是一个可选部分,由问号(“?”)分隔,其中包含本质上不是分层的附加标识信息。查询字符串语法不是一般定义的,但通常组织为一系列 = 对,这些对用分号或与号分隔。
For example:
例如:
curl http://server:5050/a/c/getName?param0=foo¶m1=bar
回答by Steven Soroka
If you really want to submit the GET request with JSON in the body (say for an XHR request and you know the server supports processing the body on GET requests), you can:
如果您真的想在正文中提交带有 JSON 的 GET 请求(比如 XHR 请求,并且您知道服务器支持处理 GET 请求的正文),您可以:
curl -X GET \
-H "Content-type: application/json" \
-H "Accept: application/json" \
-d '{"param0":"pradeep"}' \
"http://server:5050/a/c/getName"
Most modern web servers accept this type of request.
大多数现代 Web 服务器都接受这种类型的请求。
回答by user2877889
GET takes name value pairs.
GET 采用名称值对。
Try something like:
尝试类似:
curl http://server:5050/a/c/getName/?param1=pradeep
curl http://server:5050/a/c/getName/?param1=pradeep
or
或者
curl http://server:5050/a/c/getName?param1=pradeep
curl http://server:5050/a/c/getName?param1=pradeep
btw a regular REST should look something like
顺便说一句,常规的 REST 应该看起来像
curl http://server:5050/a/c/getName/pradeepIf it takes JSON in GET URL, it's not a standard way.
curl http://server:5050/a/c/getName/pradeep如果在 GET URL 中使用 JSON,则这不是标准方式。
回答by Sandeep K V
For username and password protected services use the following
对于受用户名和密码保护的服务,请使用以下内容
curl -u admin:password -X GET http://172.16.2.125:9200 -d '{"sort":[{"lastUpdateTime":{"order":"desc"}}]}'
回答by smci
Try
尝试
curl -G ...
instead of
代替
curl -X GET ...
Normally you don't need this option. All sorts of GET, HEAD, POST and PUT requests are rather invoked by using dedicated command line options.
通常你不需要这个选项。各种 GET、HEAD、POST 和 PUT 请求都是通过使用专用命令行选项来调用的。
This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. So for example if you want to make a proper HEAD request, using -X HEAD will not suffice. You need to use the -I, --head option.
此选项仅更改 HTTP 请求中使用的实际单词,它不会改变 curl 的行为方式。因此,例如,如果您想发出正确的 HEAD 请求,使用 -X HEAD 是不够的。您需要使用 -I, --head 选项。
回答by bhatman
None of the above mentioned solution worked for me due to some reason. Here is my solution. It's pretty basic.
由于某种原因,上述解决方案都不适合我。这是我的解决方案。这是非常基本的。
curl -X GET API_ENDPOINT-H'Content-Type: application/json' -d'JSON_DATA'
curl -X GET API_ENDPOINT -H'内容类型:应用程序/json' -d' JSON_DATA'
API_ENDPOINTis your api endpoint e.g: http://127.0.0.1:80/api
API_ENDPOINT是您的 api 端点,例如:http: //127.0.0.1: 80/api
-Hhas been used to added header content.
-H已用于添加标题内容。
JSON_DATAis your request body it can be something like :: {"data_key": "value"} . ' 'surrounding JSON_DATA are important.
JSON_DATA是您的请求正文,它可以类似于 :: {"data_key": "value"} 。' '围绕 JSON_DATA 很重要。
Anything after -dis the data which you need to send in the GET request
-d之后的任何内容都是您需要在 GET 请求中发送的数据

