Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL to show response headers after submiting a file

Tags:

curl

I post file to url and would like to read all headers output in curl console. What parameter should I add?

curl -F file=@"c:\Word.docx" http://do.convertapi.com/Word2Pdf > output.pdf
like image 305
Tomas Avatar asked Jul 04 '14 13:07

Tomas


People also ask

How do I show response headers in curl?

We can use curl -v or curl -verbose to display the request headers and response headers in the cURL command. The > lines are request headers . The < lines are response headers .

How do I see all response headers?

To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.

How do you pass headers in curl command?

To send an HTTP header with a Curl request, you can use the -H command-line option and pass the header name and value in "Key: Value" format. If you do not provide a value for the header, this will remove the standard header that Curl would otherwise send. The number of HTTP headers is unlimited.

What is the correct command to get only the headers for a given URL using curl command?

The -I option is used to tell curl to only fetch the HTTP headers ( HEAD method) of a particular page or resource.


2 Answers

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.

like image 94
Tim Avatar answered Oct 24 '22 18:10

Tim


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
like image 22
M. Justin Avatar answered Oct 24 '22 18:10

M. Justin