peterk
peterk

Reputation: 5444

Is it possible to have curl write it's http request to a file?

For testing HTTP protocols, I would like to use curl to make a POST (or other) request to an http server, but either have it write the request to a local file instead of the server socket, or better, simultaneously write the request to the server socket AND to a local file.

That is byte for byte what it writes to the server.

Upvotes: 0

Views: 115

Answers (1)

LMC
LMC

Reputation: 12662

Yes, it's possible on the command line at least with --trace* options

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

Output:

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

Run a minimal web server with netcat to test

 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

Upvotes: 3

Related Questions