如何使用cURL显示POST数据?


139

例如,使用-v参数发布到Web服务器:

curl -v http://testserver.com/post -d "firstname=john&lastname=doe"

和输出

> POST /post HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: testserver.com
> Accept: */*
> Content-Length: 28
> Content-Type: application/x-www-form-urlencoded
> 
< HTTP/1.1 200 OK
(etc)

没有提及我发布的数据。

cURL中是否有一个选项可在输出中显示字符串“ firstname = john&lastname = doe”?

注意:显然我想要的字符串在我执行的命令中,但是还有其他几个发布选项,例如--form和--data-ascii等。我希望看到原始数据正在发送到服务器。


1
您还可以运行tcpdump捕获要发送到服务器的实际数据。或者,如果有的话,可以使用wireshark(更好)。
基思

我不确定你能做到。这是默默无闻的安全示例吗?- stackoverflow.com/questions/198462/...
slotishtype

Answers:


175

我没有使用的最接近的方法tcpdump是使用该--trace-ascii选项:

~ curl http://w3.org/ -d "hello=there" --trace-ascii /dev/stdout
== Info: About to connect() to w3.org port 80 (#0)
== Info:   Trying 128.30.52.45... == Info: connected
== Info: Connected to w3.org (128.30.52.45) port 80 (#0)
=> Send header, 210 bytes (0xd2)
0000: POST / HTTP/1.1
0011: User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.1
0051: 9.7 OpenSSL/0.9.8l zlib/1.2.3
0070: Host: w3.org
007e: Accept: */*
008b: Content-Length: 11
009f: Content-Type: application/x-www-form-urlencoded
00d0: 
=> Send data, 11 bytes (0xb)
0000: hello=there

不幸的是,这在您发布时不起作用multipart/form-data

~ curl http://w3.org/ -F hello=there -F testing=123 --trace-ascii /dev/stdout
== Info: About to connect() to w3.org port 80 (#0)
== Info:   Trying 128.30.52.45... == Info: connected
== Info: Connected to w3.org (128.30.52.45) port 80 (#0)
=> Send header, 270 bytes (0x10e)
0000: POST / HTTP/1.1
0011: User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.1
0051: 9.7 OpenSSL/0.9.8l zlib/1.2.3
0070: Host: w3.org
007e: Accept: */*
008b: Content-Length: 244
00a0: Expect: 100-continue
00b6: Content-Type: multipart/form-data; boundary=--------------------
00f6: --------19319e4d1b79
010c: 
<= Recv header, 32 bytes (0x20)
0000: HTTP/1.1 301 Moved Permanently

4
我知道这是您自己的答案,但我认为您可以接受此作为正确答案。无论如何,它为我解决了这个问题,谢谢:-)
Darren Cook

4
删除任何-v--verbose覆盖它们的trace指令。
2014年

2
@AugustinRiedinger与https兼容。我只是尝试了一下,看到了有效载荷。数据已加密,但是由于您是连接的端点,因此您可以使用所有数据,因此curl可以看到它。
gak

2
使用--trace-ascii对我来说在OS X 10.8.5 Mountain Lion上起作用。我上传了一个包含两张图片和一个json主体的多部分表单实体,并且一切都按预期工作
Heath Borders 2014年

4
而不是--trace-ascii /dev/stdout您可以--trace-ascii -(破折号)
Adam Michalik

27

或者您可以使用https://httpbin.org/进行测试

$ curl https://httpbin.org/post -d "firstname=john&lastname=doe"
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "firstname": "john", 
    "lastname": "doe"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "27", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.43.0"
  }, 
  "json": null, 
  "origin": "78.228.163.126", 
  "url": "https://httpbin.org/post"
}

12

想添加netcat替代

#!/bin/bash
nc -l 8080 &

curl "http://localhost:8080" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--data @<(cat <<EOF
{
  "me": "$USER",
  "something": $(date +%s)
}
EOF
)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.