Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set HTTP Request "Content-Type"

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?

like image 675
Michiel Avatar asked Mar 22 '12 14:03

Michiel


2 Answers

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.

like image 129
Michael Berkowski Avatar answered Oct 29 '22 10:10

Michael Berkowski


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.

like image 28
Tony Miller Avatar answered Oct 29 '22 09:10

Tony Miller