How to resolve the error - statuscode 302?
I tried making an http callout and the response received is a redirection error as per below: System.HttpResponse[Status=Moved Temporarily, StatusCode=302] . Is there any work around to handle the redirection for HttpRequest objects in salesforce.
I also tried to make the callout in loop until we get the response but no success (as suggested HTTP Callout Error)
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); }