如何从curl获取文本格式请求?如何将curl命令转为HTTP请求文本?
Great question! Let's break this down into two clear parts: first, how to pull plain-text HTTP requests directly from curl commands, and second, converting your specific curl example into the hand-written format you’re aiming for.
Curl has built-in tools to show you exactly what it sends to the server—no external tools needed. Here are the most practical methods:
Verbose Mode (
-v):
Add the-vflag to your curl command, and look for lines starting with>in the output. These lines represent the raw request headers and body being sent. For example:curl -v https://some.api.com/$batch -H "curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)" -d '{ foo: ["bar" "baz"]}'The output will include all request details, though you’ll need to ignore the debug lines that don’t start with
>.Trace Mode for Clean Output:
For a more focused view, use--trace-ascii -to output a detailed trace, then filter to isolate just the request. A quick one-liner withgrepworks well:curl --trace-ascii /dev/stdout https://some.api.com/$batch -H "..." -d '...' | grep -A 100 '=> Send header'This will show you the full request headers followed by the body, without extra noise.
First, let’s fix a couple of issues in your original curl command and the target request:
- The
-Hvalue you provided is actually a User-Agent string—curl normally sends this automatically, but you’re overriding it here. It’s better to explicitly label it asUser-Agent:in the header for clarity. - The JSON body
{ foo: ["bar" "baz"]}is invalid (missing a comma between"bar"and"baz").
Corrected Curl Command (for valid JSON):
curl https://some.api.com/$batch \ -H "User-Agent: curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)" \ -H "Content-Type: application/json" \ -d '{"foo": ["bar", "baz"]}'
Corresponding Hand-Written HTTP Request:
POST /$batch HTTP/1.1 Host: some.api.com User-Agent: curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled) Content-Type: application/json Accept-Encoding: gzip, deflate, br Content-Length: 21 {"foo":["bar","baz"]}
If you intended to send form data instead of JSON, the Content-Type would default to application/x-www-form-urlencoded, and the body would be formatted accordingly.
For regular conversions, here’s a reliable process:
- Identify the HTTP method:
POSTif using-d/--data;GETby default; use-XforPUT/DELETE/etc. - Split the URL: Extract the
Host(e.g.,some.api.com) and request path (e.g.,/\$batch). - Map headers: Each
-Hflag becomes a request header. Remember curl auto-adds headers likeAccept-Encodingunless you override them. - Validate the body: Fix syntax errors (like missing commas in JSON), calculate
Content-Lengthas the byte count of the body, and set the correctContent-Type.
For semi-automated local conversions, you can use a simple script to clean up curl’s trace output. Here’s a one-liner that extracts just the raw request:
curl --trace-ascii - https://some.api.com/$batch -H "..." -d '...' | awk '/=> Send header/{flag=1;next}/<= Recv header/{flag=0}flag' | sed 's/^ //'
内容的提问来源于stack exchange,提问作者simone

