HTTP request headers carry metadata that a server uses to interpret a request, such as the response format a client accepts. cURL lets you send custom headers with a request to supply application-specific values or replace its default request metadata.

A custom header belongs to the request, while the server decides how to handle it. An echo service returns the received headers in its response body, making it possible to check what arrived without relying on the client's local request display.

A public echo service repeats everything it receives, sharing that data with a third-party operator. Never send real tokens, keys, or cookies to it. A header value given on the command line lands in shell history and is visible in the process list.

Steps to send custom headers with cURL:

  1. Send a request with the dummy X-Demo-Message header.
    $ curl -sS -H "X-Demo-Message: hello-demo" \
      https://httpbin.org/headers
    {
      "headers": {
        "Accept": "*/*",
        "Host": "httpbin.org",
        "User-Agent": "curl/8.18.0",
        "X-Demo-Message": "hello-demo"
      }
    }

    The uppercase -H option, also written --header, takes one quoted Name: value argument. Quoting keeps the header name and value together when they contain spaces. The -sS options hide the progress meter while keeping error messages visible.

  2. Pass a second header by adding another -H option.
    $ curl -sS -H "X-Demo-Message: hello-demo" \
      -H "X-Demo-Mode: tutorial" \
      https://httpbin.org/headers

    Each option adds a separate header to the same request. Both values here are dummy data suitable for a public echo service.

  3. Set the request's Accept header to application/json.
    $ curl -sS -H "X-Demo-Message: hello-demo" \
      -H "X-Demo-Mode: tutorial" \
      -H "Accept: application/json" \
      https://httpbin.org/headers

    cURL normally sends Accept: */*, as the first reply shows. A custom header with the same name replaces that default, so this request asks for a JSON response. The server still determines which response format it can provide.

  4. Confirm that httpbin's reply contains both demo headers and the requested Accept value.
    {
      "headers": {
        "Accept": "application/json",
        "Host": "httpbin.org",
        "User-Agent": "curl/8.18.0",
        "X-Demo-Message": "hello-demo",
        "X-Demo-Mode": "tutorial"
      }
    }

    The headers object is httpbin's report of the request it received. The matching names and values show that both custom fields and the replacement Accept header reached the server. These are request headers echoed in the response body; the server's own response headers are separate.
    Related: How to view HTTP request headers with cURL
    Related: How to show HTTP response headers with curl

Share this guide