我在使用 curl
命令时遇到问题。我想使用 JSON 参数发送请求 (GET),并且我应该从服务器得到一些答案,但不幸的是我什么也没得到。
在最好的情况下,我从服务器得到以下答案
* 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}}"
所有命令都不起作用......
浏览器中的工作命令只是:
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
最佳答案
查看工作浏览器场景在网络上发送的内容将显示如下内容:
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}}'
关于json - 使用 JSON 参数进行 Curl GET。服务器回复为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33307563/