curl request get headers

Curl Request Get Headers

If you are working with APIs or web services, you might need to make HTTP requests to fetch data from a server. One common way to do this is by using the command-line tool curl. This tool allows you to send HTTP requests using various methods, such as GET, POST, PUT, and DELETE.

If you want to fetch the headers of a response, you can use the -I option with curl. This option tells curl to fetch only the headers of the response and not the body. Here is an example:


curl -I https://example.com

This will send a GET request to https://example.com and fetch only the headers of the response. The headers will be printed in the terminal, like this:


HTTP/1.1 200 OK
Date: Tue, 15 Jun 2021 12:34:56 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Type: text/html; charset=UTF-8

You can see that the headers include various information about the response, such as the HTTP version, date, server software, and content type.

If you want to save the headers to a file instead of printing them in the terminal, you can use the -i option with curl. This option tells curl to include the headers in the output file along with the body. Here is an example:


curl -i https://example.com > response.txt

This will send a GET request to https://example.com and save the headers and body of the response to a file named response.txt.