3

I tried making an http callout and the response received is redirection error as per below: System.HttpResponse[Status=Moved Temporarily, StatusCode=302] . Is there any work around to handle the redirection for HttpRequest object in salesforce.

I also tried to make the callout in loop until we get the response but no success (as suggested HTTP Callout Error)

  • 302 is redirection! So the loop you mentioned is right way to do it. please add your code and the exact error. – AtulRajguru9 Aug 15 '16 at 16:11
12

Some HTTP client code handles this automatically but in Apex you have to do the work yourself.

Documentation about HTTP 302 explains:

The HTTP response status code 302 Found is a common way of performing URL redirection.

An HTTP response with this status code will additionally provide a URL in the location header field. The user agent (e.g. a web browser) is invited by a response with this code to make a second, otherwise identical, request to the new URL specified in the location field.

So when you get this status code you need to make a second request using the value you find in the location header of the first response e.g.:

HttpResponse res = new Http().send(req);
while (res.getStatusCode() == 302) {
    req.setEndpoint(res.getHeader('Location'));
    res = new Http().send(req);
}
| improve this answer | |

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.