cURL-如何使用GET发送HTTP请求消息正文?

时间:2019-09-20 03:24:34

标签: http curl

当我们将GET与cURL一起使用时,为什么没有请求消息正文?例如:

curl -G http://0.0.0.0:8181 -d "param1=value1&param2=value2" --trace-ascii /dev/stdout

结果:

== Info: Expire in 0 ms for 6 (transfer 0x55c11868f5c0)
== Info:   Trying 0.0.0.0...
== Info: TCP_NODELAY set
== Info: Expire in 200 ms for 4 (transfer 0x55c11868f5c0)
== Info: Connected to 0.0.0.0 (127.0.0.1) port 8181 (#0)
=> Send header, 104 bytes (0x68)
0000: GET /?param1=value1&param2=value2 HTTP/1.1
002c: Host: 0.0.0.0:8181
0040: User-Agent: curl/7.64.0
0059: Accept: */*
0066:

<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 20 bytes (0x14)
0000: Host: 0.0.0.0:8181
<= Recv header, 37 bytes (0x25)
0000: Date: Thu, 19 Sep 2019 00:34:38 GMT
<= Recv header, 19 bytes (0x13)
0000: Connection: close
<= Recv header, 56 bytes (0x38)
0000: X-Powered-By: PHP/7.3.9-1+ubuntu19.04.1+deb.sury.org+1
<= Recv header, 40 bytes (0x28)
0000: Content-type: text/html; charset=UTF-8
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 56 bytes (0x38)
0000: Array.(.    [param1] => value1.    [param2] => value2.).
Array
(
    [param1] => value1
    [param2] => value2
)
== Info: Closing connection 0

我可以看到带有POST的消息正文,例如:

curl http://0.0.0.0:8181 -d "param1=value1&param2=value2" --trace-ascii /dev/stdout

结果:

== Info: Expire in 0 ms for 6 (transfer 0x55b4a43355c0)
== Info:   Trying 0.0.0.0...
== Info: TCP_NODELAY set
== Info: Expire in 200 ms for 4 (transfer 0x55b4a43355c0)
== Info: Connected to 0.0.0.0 (127.0.0.1) port 8181 (#0)
=> Send header, 146 bytes (0x92)
0000: POST / HTTP/1.1
0011: Host: 0.0.0.0:8181
0025: User-Agent: curl/7.64.0
003e: Accept: */*
004b: Content-Length: 27
005f: Content-Type: application/x-www-form-urlencoded
0090:
=> Send data, 27 bytes (0x1b)
0000: param1=value1&param2=value2
== Info: upload completely sent off: 27 out of 27 bytes

<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 20 bytes (0x14)
0000: Host: 0.0.0.0:8181
<= Recv header, 37 bytes (0x25)
0000: Date: Fri, 20 Sep 2019 03:12:32 GMT
<= Recv header, 19 bytes (0x13)
0000: Connection: close
<= Recv header, 56 bytes (0x38)
0000: X-Powered-By: PHP/7.3.9-1+ubuntu19.04.1+deb.sury.org+1
<= Recv header, 40 bytes (0x28)
0000: Content-type: text/html; charset=UTF-8
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 56 bytes (0x38)
0000: Array.(.    [param1] => value1.    [param2] => value2.).
Array
(
    [param1] => value1
    [param2] => value2
)
== Info: Closing connection 0

您可以在上面的POST方法中看到消息正文:

=> Send data, 27 bytes (0x1b)
0000: param1=value1&param2=value2
== Info: upload completely sent off: 27 out of 27 bytes

但是GET方法中没有任何内容。我们如何显示带有GET消息的消息正文?

有什么想法吗?

编辑:

$ curl http://0.0.0.0:8181 -d "param1=value1&param2=value2" --trace-ascii - -X GET
== Info: Expire in 0 ms for 6 (transfer 0x55bf2e32d5c0)
== Info:   Trying 0.0.0.0...
== Info: TCP_NODELAY set
== Info: Expire in 200 ms for 4 (transfer 0x55bf2e32d5c0)
== Info: Connected to 0.0.0.0 (127.0.0.1) port 8181 (#0)
=> Send header, 145 bytes (0x91)
0000: GET / HTTP/1.1
0010: Host: 0.0.0.0:8181
0024: User-Agent: curl/7.64.0
003d: Accept: */*
004a: Content-Length: 27
005e: Content-Type: application/x-www-form-urlencoded
008f: 
=> Send data, 27 bytes (0x1b)
0000: param1=value1&param2=value2
== Info: upload completely sent off: 27 out of 27 bytes
<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 20 bytes (0x14)
0000: Host: 0.0.0.0:8181
<= Recv header, 37 bytes (0x25)
0000: Date: Fri, 20 Sep 2019 15:07:34 GMT
<= Recv header, 19 bytes (0x13)
0000: Connection: close
<= Recv header, 56 bytes (0x38)
0000: X-Powered-By: PHP/7.3.9-1+ubuntu19.04.1+deb.sury.org+1
<= Recv header, 40 bytes (0x28)
0000: Content-type: text/html; charset=UTF-8
<= Recv header, 2 bytes (0x2)
0000: 
<= Recv data, 10 bytes (0xa)
0000: Array.(.).
Array
(
)
== Info: Closing connection 0

1 个答案:

答案 0 :(得分:2)

由于-G,您要求curl转换-d数据并将其放入URL查询中。

要解决此问题,请删除-G,保留-d,但用GET强制执行-X

curl http://0.0.0.0:8181 -d "param1=value1&param2=value2" --trace-ascii - -X GET