2

I have a problem with curl command. I want to send request (GET) with JSON parametr, and I should get some answer from server but unfortunately I nothing get. In the best case I get below answer form server

* 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

I read below topics:

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

So I have tryed below command

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}}"

All command doesn't works....

Works command in browser is just:

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

edit:

Below answer for question

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
Community
  • 1
  • 1
Mbded
  • 1,754
  • 4
  • 23
  • 43
  • 1
    I'd recommend to properly percent-escape parameter values in urls, ie. `192.168.1.101/ckk.cgi?json=%7B"asking1"%3A%7B"001"%3A"givnames1","_id"%3A223%7D%7D` (colons and opening/closing braces escaped). [RFC 3986](https://tools.ietf.org/html/rfc3986) is the relevant standard. – collapsar Oct 23 '15 at 16:50
  • I tryed `curl -v -H "Accept: application/json" -H "Content-type: application/json" -X GET -d '%7B"asking1"%3A%7B"001"%3A"givnames1","_id"%3A223%7D‌​%7D' http://192.168.1.101/ckk.cgi` then I get this same answer as above, and also tryed just `curl 192.168.1.101/ckk.cgi?json=%7B"asking1"%3A%7B"001"%3A"givnames1","_id"%3A223%7D‌​%7D` then I do not get nothing. – Mbded Oct 23 '15 at 17:14
  • 1
    Next time try to use `curl -vvv`. It will present an extremely verbose output that will probably help you to find a solution. – Daniel Oct 24 '15 at 05:19
  • I added content display after used -vv in **edit** – Mbded Oct 24 '15 at 05:54

1 Answers1

3

Looking in to what gets sent on the wire by the working browser scenario would display something like:

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

i.e. the JSON part gets URL-encoded (well at least partly). The same effect can by achieved using --data-urlencode and -G flags:

curl -G -v "http://192.168.1.101/ckk.cgi" --data-urlencode 'json={"asking1":{"001":"givnames1","_id":223}}'
Hans Z.
  • 50,496
  • 12
  • 102
  • 115