cURL - как отправить тело сообщения HTTP-запроса с помощью GET? - PullRequest
0 голосов
/ 20 сентября 2019

Почему нет тела сообщения запроса, когда мы используем 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 Ответ

2 голосов
/ 20 сентября 2019

Поскольку с -G вы просили curl преобразовать данные -d и вместо этого поместить их в URL-запрос.

Чтобы исправить, удалите -G, сохраните -d, но принудительно введите GETс -X:

curl http://0.0.0.0:8181 -d "param1=value1&param2=value2" --trace-ascii - -X GET
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...