10

How do you set the content type of an HTTP Request?

I've tried this:

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

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

And this is the result:

HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Type: text/html;charset=UTF-8
Date: Thu, 22 Mar 2012 14:04:36 GMT

but no luck so far...
What do I have to do to get an Content-Type: application/xml in my HTTP response?

6
  • Yes I know, but what do I need to set in my request to get an Content-Type: application/xml response? Commented Mar 22, 2012 at 14:08
  • The result you give is an HTTP response not a request. Are you asking how to set the content-type of the request, or are you asking how to ask the server to provide XML in the response? Commented Mar 22, 2012 at 14:08
  • You need to set the Accept header (and not the content-type header). Then the server needs to support multiple response types and use the accept header to determine which one to use. Do you know if the server does that? Commented Mar 22, 2012 at 14:09
  • I'm asking what do I have to set in my request to have an Content-Type: application/xml in my respons Commented Mar 22, 2012 at 14:10
  • @Quentin, I do set the Accept header, no? Commented Mar 22, 2012 at 14:10

2 Answers 2

29

I believe the headers must be a plain array whose elements are full key:value headers, not an associative array:

$headers = array();
$headers[] = 'Accept: application/xml';
$headers[] = 'Content-Type: application/xml';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

This is specified in the curl_setopt() documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for trying, but still the same result. Or do I have to clear something? Coockies, cache,...
Is that the HTTP response back? You are sending the content-type header in your request. That doesn't mean the responding server will send back the same content-type.
I've tried the same request in WizTools and the server gave back the application/xml Content-Type, so the server is capable of sending the right response. Only when I try with custom PHP, I don't get to send the Content-Type settings...
2

You set an accept type for your request (and use Michael's answer for that, you should count his answer as answering the direct question, because it does). That doesn't mandate that the server resonds with that content type.

If the other end of the request is a static file, you have to make sure your web server sends that file with a MIME type of application/xml. If it ends with .xml, then both Apache and IIS will already do this. If it's something else that is a non-standard file extension, but you want it to be sent as application/xml, then you'll have to get the server manager to set the httpd.conf or .htaccess to add the mime type for the file. In IIS you use the GUI admin tools to do the same thing, adding a mime type for the file extension as application/xml.

If the other end of the request is a server side scripting language such as PHP, Perl, Python, ColdFusion, ASP, ASP.net, etc etc then you need to use the appropriate method/function in that language for the script being called to emit the content type header and set it to application/xml.

Update: You say in comments you're using WizTools to emit a request that does get a return of application/xml. If you want to clone that environment, then send ALL the headers it's sending in your curl request. One guess is that user agent might be in play.

1 Comment

Thanks for this valuable answer. So, the request is fine as it is, it's the server that's not working along, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.