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. Commented Aug 15, 2016 at 16:11

1 Answer 1


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) {
    res = new Http().send(req);
  • Is this code snippet should be added in constructor of the Apex class? Commented Apr 4, 2021 at 15:27
  • 1
    Hi @RuklaniAttanyake, It should be added in whatever method you want to send the message from. It makes more than one request following the redirect locations it receives with the last request actually doing the actual work.
    – Keith C
    Commented Apr 4, 2021 at 15:53
  • Thank you @KeithC. In my scenario, I have two tabs open simultaneously in the browser which are community visualforce pages. When the timeout happens, the first tab goes to the logout page and from a javascript function it is redirecting to the login page. Upto that it is working fine. After this , in the second tab browser console I observed getting too many 302 erros and when the time out happens it gives 431(Header field too large ) error. Any thoughts on this? Although I tried several things , still couldn't figure it out Commented Apr 4, 2021 at 16:28
  • @RuklaniAttanyake, Suggest you use your browser's developer tools (network tab) to examine how the header grows. But I don't know why this combination fails in that way.
    – Keith C
    Commented Apr 5, 2021 at 8:30
  • 1
    Hi @Gerke, My assumption is that the redirect Url will be blocked. But best Google or try it to be sure.
    – Keith C
    Commented Oct 6, 2021 at 16:56

You must log in to answer this question.

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