Fetch only content-type using cURL Command


If you only want to fetch the content-type for a response, make use of the --write-out option and the variable content_type

Example:
% curl -s -o /dev/null -w "%{content_type}\n" http://example.org
Output:

text/html; charset=UTF-8

cURL get Content-Type

You may also make use of the -I option along with grep,

% curl -I http://example.org                                 
HTTP/1.1 200 OK
Content-Encoding: gzip
Accept-Ranges: bytes
Age: 584814
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Mon, 12 Dec 2022 11:33:10 GMT
Etag: "3147526947"
Expires: Mon, 19 Dec 2022 11:33:10 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (dcb/7F60)
X-Cache: HIT
Content-Length: 648

Example:
% curl -Is http://example.org | grep -iF 'Content-Type'
Output:

Content-Type: text/html; charset=UTF-8

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap