I have the below code trying to make a HTTP callout from a APEX class. The response I get back is

System.HttpResponse[Status=Moved Temporarily, StatusCode=302]

However if I use the same URL to make the callout form a REST client in firefox I get the desired JSON body back. Can you please advise what I am doing wrong in the below code in salesforce ?

     HttpRequest req = new HttpRequest();

     String username = 'XXXXXXXXX';
     String password = 'YYYYYYYYYY';

     Blob headerValue = Blob.valueOf(username + ':' + password);
     String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
     System.debug( 'authorizationHeader : ' + authorizationHeader );
     req.setHeader('Authorization', authorizationHeader);

     Http http = new Http();
     HTTPResponse res = http.send(req);

The return message that I get back from the Apache server has the new url in it. It is the same url as above but with the https. I am still confused with the original call though. Why does normal browser or another rest client show me the response with status code 200 and salesforce shows status code 302

  • Do you have remote site configured for your domain you're trying to call? Any exceptions in your debug logs? Sep 15, 2014 at 1:29
  • Remote site is configured for both http and https. No exceptions in debug log
    – HSG
    Sep 15, 2014 at 1:29
  • 3
    Look at this question: salesforce.stackexchange.com/questions/11126/… Sep 15, 2014 at 1:41
  • Out of curiosity, the url http://host-name/is just a placeholder to avoid displaying the real url right? or is it the real url you're reaching? Sep 15, 2014 at 1:41
  • yes.. it is just a placeholder for the actual host name.
    – HSG
    Sep 15, 2014 at 1:44

2 Answers 2


You are doing nothing wrong. The HTTP request object provided by Salesforce does not handle 302 redirects like your browser does.

You can try handling redirects by running the callout again on the redirected site, as shown in this answer, or you can use a tool to find out what the ultimate endpoint is, like redirect detective.

As long as you are able to hit this ultimate end-point site in your browser, you should be able to query it from the Salesforce HTTP object. Make your HTTP object query it, and it should all work.


To fix this 302 error -- use below code

if (res.getStatusCode() >=300 && res.getStatusCode() <= 307 && res.getStatusCode() != 306) {
                    do {
                        redirect = false; // reset the value each time
                        String loc = res.getHeader('Location'); // get location of the redirect
                        if(loc == null) {
                            redirect = false;
                        req = new HttpRequest();
                        res = http.send(req);
                        if(res.getStatusCode() != 500) { // 500 = fail
                            if(res.getStatusCode() >=300 && res.getStatusCode() <= 307 && res.getStatusCode() != 306) {
                                redirect= true;
                            // I do special handling here with cookies
                            // if you need to bring a session cookie over to the
                            // redirected page, this is the place to grab that info
                            system.debug('Inside this if');
                    } while (redirect && Limits.getCallouts() != Limits.getLimitCallouts());


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .