4

So you want to call your Apex classes from outside salesforce via RESTful HTTP and need XML resurned even though it's uncool. The official documentation suggests it's supported by simply adding the Accept: text/xml header to your requests. My tests today show this not to be the case. Indeed this header does nothing to get you XML back and there are 2 different ways depending on whether you are doing a login or calling your code.

If you use Accept: text/xml without the tricks described below you will not get XML. For the login (/token) service you get JSON but for calling your own apex you get the following error:

[{"message":"Accept header specified in HTTP request is not supported: text/xml","errorCode":"NOT_ACCEPTABLE"}]

I realise the solutions below are documented in various places but I hope having the answer here in one place (with error messages) helps others!

4

1. Login Procedure

For the login procedure POST is used. Here you have to add the parameter format=xml to your request body.

POST https://test.salesforce.com/services/oauth2/token HTTP/1.0
content-type: application/x-www-form-urlencoded; charset=utf-8
content-length: 256
accept: */*
user-agent: Whateva
host: test.salesforce.com

grant_type=password&format=xml&client_id=***.***&client_secret=***&username=user%40domain.com.dev1&password=*****TOKEN

This will get you your "token" or session id which you pass below as the Authorization header.

2. Calling Classes

Here, again, adding an Accept header does nothing. Instead add the .xml suffix to your class name (so the class zrest.cls is called as zrest.xml):

GET https://cs8.salesforce.com/services/apexrest/zrest.xml HTTP/1.0
content-length: 0
authorization: OAuth ****!****
user-agent: Whateva
host: cs8.salesforce.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.