Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl show Content-Type only

Tags:

linux

curl

I was wondering if it is possible to use curl to only show the content-type of the response header.

I want to check if the content-type is text/html for example before downloading instate of downloading the file and then find out it is application/pdf.

I used the example below in the hope that it would return the document if it is valid for me and else do nothing or something! The sample below just prints the full content of the page.

curl -F "type=text/html" www.google.nl

But If i do something like the example below it still downloads the whole thing, and I don't think that is right...

curl -F "type=text/html" http://www.axmag.com/download/pdfurl-guide.pdf

Many thanks :D

like image 983
bobbybouwmann Avatar asked May 15 '14 10:05

bobbybouwmann


People also ask

How do I specify Content-Type in curl?

Setting the Content-Type of a Curl Request. To send the Content-Type header using Curl, you need to use the -H command-line option. For example, you can use the -H "Content-Type: application/json" command-line parameter for JSON data. Data is passed to Curl using the -d command-line option.

How do I make my header only curl?

Display only Response Headers in curl If you want to display only the response headers, you can use the --head flag.

How do I see curl request headers?

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 .


1 Answers

Option -F is for forms. Instead you want to send a HEAD request for retrieving only the response header without the response body by using option -I.

To display an URL's content type:

curl -s -I www.google.nl | grep -i "^Content-Type:"

Here option -s is added for silent mode for excluding the progress meter and error messages.

You can also specify the Accept header in your HTTP request. This header is used to accept only specific content types:

curl -s -H "Accept: text/html" http://www.axmag.com/download/pdfurl-guide.pdf

But the disadvantage is that most webservers will serve you an error page which also has the content type text/html. Hence you will still get a HTML file.

like image 200
scai Avatar answered Nov 08 '22 10:11

scai