PHP cURL Content-Type is not set
When debugging HTTP requests in PHP, it is easy to assume that passing a header array to curl_setopt guarantees the server receives those headers. However, if your target server returns a "Bad Request" error specifically because the Content-Type header is missing or incorrect, looking at curl_getinfo($ch) for the content_type index will often yield NULL. This discrepancy between what you configure and what actually gets sent usually points to a specific configuration conflict rather than a bug in cURL itself.
In the provided scenario, the developer sets up a POST request to send XML data. The code explicitly defines the headers:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml',
'Connection: Keep-Alive'
));
The server responds with HTTP 400 Bad Request, and inspection of the request info shows "content_type" => NULL. Despite setting CURLOPT_POSTFIELDS to the XML string, cURL does not send the header. The root cause here is a subtle interaction between how cURL handles headers for POST requests and how it defaults to application/x-www-form-urlencoded when certain conditions are met.
The Default Content-Type Trap
cURL has a built-in mechanism to infer the Content-Type header automatically if you do not provide one explicitly. If CURLOPT_POSTFIELDS contains an associative array (key-value pairs), cURL sets the content type to application/x-www-form-urlencoded. If it is a string, cURL typically defaults to application/octet-stream.
However, there are specific flags that override this behavior or prevent the automatic header injection even when you attempt to set one manually. The most common culprit in this situation is the combination of CURLOPT_POSTFIELDS containing a raw string and the absence of CURLOPT_HTTPHEADER handling for the content type in older cURL versions, or more critically, the interaction with CURLOPT_POST.
When CURLOPT_POST is enabled (set to 1), cURL prepares to send a POST request. If you do not explicitly define the Content-Type via CURLOPT_HTTPHEADER, cURL attempts to guess it based on the data type of $xml. If $xml is a simple string, cURL might decide to omit the header entirely or set it to application/octet-stream. Some strict legacy servers reject any POST request that lacks an explicit Content-Type declaration, treating the absence of the header as equivalent to sending the wrong type.
The "Expect" Header Interference
A more insidious issue in the provided code snippet is the line:
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect: "));
This explicitly adds an Expect header with a trailing space. While this isn't strictly forbidden, it indicates that the developer is manually managing headers outside of the standard array. More importantly, if there are conflicting configurations or if the server interprets the request based on specific RFC compliance regarding content length and type, manual header injection can sometimes be overridden or ignored depending on how the client library is initialized.
However, the primary reason for the NULL content-type in curl_getinfo despite setting the header is often that cURL failed to inject it because it detected a conflict or simply defaulted due to the way CURLOPT_POSTFIELDS was handled in conjunction with other flags.
Correcting the Request
To ensure the server receives application/xml, you must explicitly force cURL to treat the payload as such. The most reliable method is to ensure that CURLOPT_HTTPHEADER is set correctly and that no other options are interfering.
If you are sending a raw XML string, you should verify that CURLOPT_POSTFIELDS is indeed a string and not an array. If it is a string, cURL generally does not add a default Content-Type unless forced. Therefore, your manual header setting is correct in principle, but the execution context might be stripping it.
Here is a refined approach to ensure the header persists:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://192.168.1.37'); // Ensure URL includes protocol
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
// Define headers explicitly as an array of strings
$headers = [
'Content-Type: application/xml',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Ensure we don't let cURL guess the content type automatically
// This is usually default behavior, but being explicit helps with strict servers.
curl_setopt($ch, CURLOPT_HEADER, 0); // Disable header output in response to avoid confusion
$response = curl_exec($ch);
Why curl_getinfo Might Show NULL
The curl_getinfo() function returns information about the request as it was sent or the state of the handle. If the handle is initialized but not yet executed, or if an error occurs before the headers are finalized, the content_type field might remain unset in the return array. In your original code, you call curl_getinfo($ch) immediately after setting options but before executing the request logic fully or handling potential initialization errors.
Furthermore, if the server rejects the connection (HTTP 400) very quickly, some cURL implementations may not populate all fields in the request array returned by getinfo until the transaction is fully processed or fails gracefully. The NULL value suggests that the request body was sent without the header, causing the immediate rejection, which prevents cURL from updating the info array with the intended headers it wanted to send.
Best Practices for Strict Servers
When dealing with legacy or strict web services that mandate specific content types:
- Always use
CURLOPT_HTTPHEADER: Do not rely on cURL's automatic detection. - Check the protocol: Ensure your URL includes
http://orhttps://. In the source material, the URL was just an IP address (192.168.1.37). Without a scheme, some PHP configurations or proxies might fail to interpret the request correctly, leading to header stripping. - Disable automatic redirects: Use
CURLOPT_FOLLOWLOCATIONonly if necessary, as redirects can sometimes alter headers depending on server configuration.
By explicitly forcing the URL schema and ensuring the headers array is passed cleanly without interference from unexpected flags like malformed Expect headers, you guarantee that the Content-Type: application/xml header reaches the target server.
