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 is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

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