是否可以将curl的http请求写入文件?

时间:2019-05-02 20:36:46

标签: http curl

为了测试HTTP协议,我想使用curl来向http服务器发出POST(或其他)请求,但是要么让它将请求写入本地文件而不是服务器套接字,要么更好地同时写入服务器套接字和本地文件的请求。

这是逐字节写入服务器的内容。

1 个答案:

答案 0 :(得分:3)

是的,至少可以在命令行上使用--trace*选项

curl --trace-time --trace-ascii - -d "some=val&other=123" http://localhost:8000

输出:

20:11:26.176533 == Info: Rebuilt URL to: http://localhost:8000/
20:11:26.180738 == Info:   Trying 127.0.0.1...
20:11:26.180775 == Info: TCP_NODELAY set
20:11:26.180903 == Info: Connected to localhost (127.0.0.1) port 8000 (#0)
20:11:26.180969 => Send header, 148 bytes (0x94)
0000: POST / HTTP/1.1
0011: Host: localhost:8000
0027: User-Agent: curl/7.60.0
0040: Accept: */*
004d: Content-Length: 18
0061: Content-Type: application/x-www-form-urlencoded
0092: 
20:11:26.181019 => Send data, 18 bytes (0x12)
0000: some=val&other=123
20:11:26.181029 == Info: upload completely sent off: 18 out of 18 bytes
20:11:26.181069 <= Recv header, 22 bytes (0x16)
0000: HTTP/1.1 201 CREATED
20:11:26.181091 <= Recv header, 19 bytes (0x13)
0000: Connection: Close
20:11:26.181099 <= Recv header, 2 bytes (0x2)
0000: 
20:11:26.181104 <= Recv data, 12 bytes (0xc)
0000: .2019-05-02.

2019-05-02
20:11:26.181147 == Info: Closing connection 0

使用netcat运行最小的Web服务器进行测试

 while true ; do { echo -e "HTTP/1.1 201 CREATED\r\nConnection: Close\r\n\r\n"; date --iso-8601 ; } | netcat -q 0 -l 8000 ;done