Curl GET与JSON参数。来自服务器的空回复

时间:2015-10-23 16:46:59

标签: json curl

我遇到curl命令的问题。我想发送请求(GET)与JSON参数,我应该从服务器得到一些答案但不幸的是我没有得到。 在最好的情况下,我得到答案表单服务器

* Hostname was NOT found in DNS cache
*   Trying 192.168.1.101...
* Connected to 192.168.1.101 (192.168.1.101) port 80 (#0)
> GET /ckk.cgi HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 192.168.1.101
> Accept: application/json
> Content-type: application/json
> Content-Length: 41
> 
* upload completely sent off: 41 out of 41 bytes
* Empty reply from server
* Connection #0 to host 192.168.1.101 left intact
curl: (52) Empty reply from server

我阅读以下主题:

Curl GET request with json parameter How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

所以我尝试了以下命令

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET 192.168.1.101/ckk.cgi?json={"asking1":{"001":"givnames1","_id":223}}

curl -H "Content-Type: application/json" -X GET -d '{"asking1":{"001":"givnames1","_id":223}}' http://192.168.1.101/ckk.cgi?

curl -X GET -H "application/json" -d '{"asking1":{"001":"givnames1","_id":223}}' http://192.168.1.101/ckk.cgi?

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X GET -d '{"asking1":{"001":"givnames1","_id":223}}' http://192.168.1.101/ckk.cgi?

curl -i GET -H "Accept: application/json" "http://192.168.1.101/ckk.cgi??json={"asking1":{"001":"givnames1","_id":223}}"

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET -d '{"asking1":{"001":"givnames1","_id":223}}}' 

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET -d "{"asking1":{"001":"givnames1","_id":223}}" http://192.168.1.101/ckk.cgi?

curl --data "{"asking1":{"001":"givnames1","_id":223}}}" http://192.168.1.101/ckk.cgi?

curl -v  http://192.168.1.101/ckk.cgi? --header "Content-Type: application/json" -d "{"asking1":{"001":"givnames1","_id":223}}"

所有命令都不起作用....

浏览器中的Works命令只是:

192.168.1.101/ckk.cgi?json={"asking1":{"001":"givnames1","_id":223}}

修改

问题的答案

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET 192.168.1.101/ckk.cgi?json={"asking1":{"001":"givnames1","_id":223}}

HTTP/1.1 200 OK
Date: Sat, 24 Oct 2015 04:58:11 GMT
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.2c
Content-Length: 0
Content-Type: text/html;charset=utf-8

HTTP/1.1 200 OK
Date: Sat, 24 Oct 2015 04:58:16 GMT
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.2c
Content-Length: 0
Content-Type: text/html;charset=utf-8

 curl -vv -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET 192.168.1.101/ckk.cgi?json={"asking1":{"001":"givnames1","_id":223}}

* Hostname was NOT found in DNS cache
*   Trying 192.168.1.101...
* Connected to 192.168.1.101 (192.168.1.101) port 80 (#0)
> GET /skk.cgi?json=asking1:001:givname1 HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 192.168.1.101
> Accept: application/json
> Content-Type: application/json
> 
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Sat, 24 Oct 2015 05:33:39 GMT
Date: Sat, 24 Oct 2015 05:33:39 GMT
* Server Apache/2.4.12 (Unix) OpenSSL/1.0.2c is not blacklisted
< Server: Apache/2.4.12 (Unix) OpenSSL/1.0.2c
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.2c
< Content-Length: 0
Content-Length: 0
< Content-Type: text/html;charset=utf-8
Content-Type: text/html;charset=utf-8

< 
* Connection #0 to host 192.168.1.101 left intact
* Found bundle for host 192.168.1.101: 0x7f53c7dbb5a0
* Re-using existing connection! (#0) with host 192.168.1.101
* Connected to 192.168.1.101 (192.168.1.101) port 80 (#0)
> GET /ckk.cgi?json=asking:_id:223 HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 192.168.1.101
> Accept: application/json
> Content-Type: application/json
> 
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Sat, 24 Oct 2015 05:33:44 GMT
Date: Sat, 24 Oct 2015 05:33:44 GMT
* Server Apache/2.4.12 (Unix) OpenSSL/1.0.2c is not blacklisted
< Server: Apache/2.4.12 (Unix) OpenSSL/1.0.2c
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.2c
< Content-Length: 0
Content-Length: 0
< Content-Type: text/html;charset=utf-8
Content-Type: text/html;charset=utf-8

< 
* Connection #0 to host 192.168.1.101 left intact

1 个答案:

答案 0 :(得分:2)

通过工作浏览器方案查看在线上发送的内容将显示如下内容:

192.168.1.101/ckk.cgi?json={%22asking1%22:{%22001%22:%22givnames1%22,%22_id%22:223}}

即。 JSON部分获得URL编码(至少部分)。使用--data-urlencode-G标志可以实现相同的效果:

curl -G -v "http://192.168.1.101/ckk.cgi" --data-urlencode 'json={"asking1":{"001":"givnames1","_id":223}}'