cURL to show response headers after submiting a file

In Unix and Unix-like systems the following submits the request, writing the response headers to the console and saving the response body to output.pdf:

curl -F file=@"c:\Word.docx" http://do.convertapi.com/Word2Pdf \
--output output.pdf --dump-header /dev/fd/1 --silent | tail -n +2

Explanation

The --output output.pdf option outputs the results to output.pdf.

-o, --output <file>
       Write output to <file> instead of stdout.

The --dump-header /dev/fd/1 option writes the HTTP status line (e.g. HTTP/1.1 200 OK) and response headers to standard output (i.e. the console). The --dump-header option dumps the headers to the given file. /dev/fd/1 is the file descriptor for stdout, so anything written to it is output to the console (or to wherever the standard output is piped or redirected).

-D, --dump-header <filename>
       (HTTP FTP) Write the received protocol headers to the specified file.

The --silent option makes it so that just the headers are output, and not the progress bar.

-s, --silent
       Silent  or  quiet  mode. Don't show progress meter or error messages.  Makes Curl mute. It will still output the data you ask for,
       potentially even to the terminal/stdout unless you redirect it.

The result of curl is piped to tail command tail -n +2, which discards the first line of content, which is the HTTP status line (e.g. HTTP/1.1 200 OK). If you want or don't mind having the status line as well as the headers, this tail pipe portion can be omitted.

-n, --lines=[+]NUM
       output the last NUM lines, instead of the last 10; or use
       -n +NUM to output starting with line NUM

Example

$ curl http://www.example.com --output test.html \
--dump-header /dev/fd/1 --silent | tail -n +2

Age: 466166
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Wed, 27 Oct 2021 16:49:42 GMT
Etag: "3147526947+gzip+ident"
Expires: Wed, 03 Nov 2021 16:49:42 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (chb/0286)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256

Use -i

From the cURL manual

 -i, --include       Include protocol headers in the output (H/F)

Note also:

-I, --head          Show document info only

The first will show headers, followed by body. The second will send a HEAD request so can't be used in your example as you're POSTing data.

Edit

The header output using -i is echoed to stdout, the same as the request body so directing the response into a PDF file will create an invalid PDF.

So I suggest instead you use -v which will be much noisier, but will show headers on command line when directing stdout to file, because verbose output goes to stderr.

Tags:

Curl