【问题标题】:cURL - how to send HTTP Request message body with GET?cURL - 如何使用 GET 发送 HTTP 请求消息正文?
【发布时间】:2019-09-20 23:08:35
【问题描述】:

为什么当我们使用 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

【问题讨论】:

    标签: http curl


    【解决方案1】:

    因为使用 -G,您要求 curl 转换 -d 数据并将其放入 URL 查询中。

    要修复,删除-G,保留-d,但使用-X 强制执行GET

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

    【讨论】:

    • 感谢您的回答,但数据"param1=value1&amp;param2=value2" 根本没有发送。见上面的 mu 编辑
    • 再看一遍? =&gt; Send data, 27 bytes (0x1b) 0000: param1=value1&amp;param2=value2
    • 对不起,我的意思是在响应中,数组是空的0000: Array.(.).
    • 同样在请求中,0000: GET / HTTP/1.1应该是0000: GET /?param1=value1&amp;param2=value2 HTTP/1.1
    • 您实际上是在发送一个帖子数据,您可以在005e: Content-Type: application/x-www-form-urlencoded这一行看到它所说的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2015-04-27
    • 2019-08-15
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多