+ Start a Discussion
sgkmillssgkmills 

How to Set the HttpResponse object Content-Type?

I am trying to do a callout that logon's to a webservice and then returns an xml file.  I am trying to set the response header's content-type to be application-xml, but only see a setHeader method on the HttpRequest object.  

 

I am having a problem setting the Content-Type header with the below code.  When I check the Content-Type of the HttpResponse object, the value isn't 'application/xml' as I expected. 

 

Is this the correct approach?

 

 

Http http = new Http(); HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setHeader('Content-Type', 'application/xml'); req.setEndpoint('https://something./rest/clientauth/authenticate?account=xxx&password=xxx&source=xxx'); try { //Execute web service call here HTTPResponse res = http.send(req); //Helpful debug messages System.debug(res.toString()); System.debug('STATUS:'+res.getStatus()); System.debug('STATUS_CODE:'+res.getStatusCode()); System.debug('Content-Type:'+ res.getHeader('Content-Type')); } catch (System.CalloutException e) { system.debug('SKM-CalloutException occurred'); }

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
danwilkiedanwilkie

Hi

 

Try setting the following header on the request before you send it:

 

req.setHeader('Accept', 'application/xml');

 

Hope it helps.

 

Dan

All Answers

danwilkiedanwilkie

Hi

 

Try setting the following header on the request before you send it:

 

req.setHeader('Accept', 'application/xml');

 

Hope it helps.

 

Dan

This was selected as the best answer
sgkmillssgkmills

This worked!  :smileyhappy:  Thanks!  Is this documented somewhere in Salesforce documentation because I was reading a bunch of articles on the wiki and didn't find anything.

 

 

bbrantly1bbrantly1

Here is a list of header request, it briefly touches on all the possibilities

 

 http://en.wikipedia.org/wiki/List_of_HTTP_headers