Why does the below code always get a "500 internal server error" back from ExactTarget when run anonymously from the Salesforce developer console? (Of course I'm using my own client id and client secret.)

Posting this exact same JSON over to the same URL using the REST Console Chrome app works great. I get the token back. I think I must be leaving some step out of the below apex code. Any ideas?

public static HttpResponse ETPost(string URL, string Body) {
    HttpRequest request = new HttpRequest();
    Http http = new Http();
    HttpResponse response = new HttpResponse();
    try {
        response = http.send(request);
    Catch(System.CalloutException e) {
        System.debug('Callout error: ' + e);
    return response;

string tokenURL = 'https://auth.exacttargetapis.com/v1/requestToken';
string JSON = '{"clientId": "My client ID","clientSecret": "My client secret"}';

HttpResponse response = ETPost(tokenURL, JSON);

I was missing the following line of code: request.setHeader('Content-Type', 'application/json');

Once I added it under the request.setBody(Body); line, it worked like a charm.

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.