Set HTTP Request "Content-Type"

Set HTTP Request "Content-Type"

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Distinction Between Request and Response Headers

A common misconception in HTTP development is that setting a header on a request automatically dictates the Content-Type of the response you receive from the server. The code snippet provided in your query demonstrates exactly why this logic fails:

$headers['Accept'] = 'application/xml';
$headers['Content-Type'] = 'application/xml';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

In this example, you are telling the client (cURL) to send a request with Content-Type: application/xml and to accept responses in XML format. However, the server responds with Content-Type: text/html;charset=UTF-8. This discrepancy exists because HTTP headers operate on a strict boundary between what you ask for and what the server decides to give back.

When you set Content-Type in your outgoing request, you are declaring the media type of the body you are sending to the server. If you are posting form data or uploading a file, this header tells the server how to parse that incoming payload. It does not instruct the server on what format it should return for its own response.

To influence the Content-Type of the HTTP response, you must look at the request headers related to acceptance and negotiation, not declaration. The most relevant header here is Accept. By setting Accept: application/xml, you are signaling your client's preference. However, this is merely a suggestion. The server retains full autonomy to decide what it returns based on its internal logic, routing rules, or content negotiation headers like Accept-Language or specific query parameters.

If your goal is to receive an XML response, relying solely on the Accept header is often insufficient if the server lacks a mechanism to honor client hints. Many older or rigid web servers default to returning HTML regardless of the Accept header unless explicitly configured otherwise. In such cases, you cannot force the server's output format purely through standard HTTP request headers; that control must reside on the server side (e.g., via .htaccess, Nginx configuration, or application logic).

When Does Request Content-Type Matter?

Setting Content-Type in your request is critical when you are sending a non-empty body. For instance, if you are using POST to submit form data:

$postData = [
    'username' => 'john_doe',
    'password' => 'secret123'
];

// Without Content-Type, cURL might default to application/x-www-form-urlencoded or text/plain depending on context.
$headers['Content-Type'] = 'application/json'; 

Here, Content-Type: application/json ensures the server parses the body as a JSON object rather than trying to read it as form fields. If you omit this and send raw JSON without setting the header, the server might reject the request or fail to map the data correctly.

The pitfall in your original scenario is conflating the input format with the output format. The Content-Type of the response is determined by:

  1. Server Configuration: Static routing rules (e.g., /api/* returns JSON, /users/* returns HTML).
  2. Content Negotiation: Headers like Accept, Accept-Language, and Accept-Encoding.
  3. Application Logic: The script explicitly sets the response header before sending data.

Achieving Your Goal

If you are developing a server (using PHP, for example) and want to ensure the response is XML when the client requests it, you must set the header on the response side, not the request side:

// Inside your server-side script
header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<user><name>John</name></user>';

If you are strictly a client using cURL and the server refuses to respect the Accept header, there is no standard HTTP workaround that forces the server to change its response type. You may need to inspect the server's documentation or use specific query parameters (like ?format=xml) if the API supports them.

For robust API interactions where you need guaranteed formatting, frameworks often handle these negotiations automatically. If you are working within a Laravel ecosystem, utilizing packages that standardize API responses can simplify this process, as they often include built-in logic to respect Accept headers and return appropriate content types without manual header manipulation for every endpoint.

Ultimately, remember the separation of concerns: your request headers describe what you are sending and what you want; the server's response headers describe what it is actually sending.

Our network