question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling 302 status/redirect

See original GitHub issue

I’ve posted this on stackoverflow but I’ll ask here as well.

I have a POST request which returns a 302 when successful. I need to get the location response header which holds the url I’ll need to redirect to. Currently I have this:

accountLogin: builder.mutation<any, any>({
   query: (body) => ({
      url: `/accounts/login`,
      method: 'POST',
      body,
      responseHandler: (response) => {
        console.log(response, response.headers);
        return response.text(); //.text() is just for testing purposes
      },
  }),
}),

If I look in the network tab of my browser the 302 is there, as is the location header with the proper URL. But the ‘console.log’ in responseHandler shows no headers. I can’t find much about this in the documentation – what am I missing?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
phryneascommented, Oct 25, 2022

fetchBaseQuery accepts all options that you can also pass into fetch and passes those into fetch.

If fetch follows, that happens internally within that, so RTK Query will never even see the 302.

See https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#parameters

Essentially: try to make the fetch call by hand - if that works it should also work with RTKQ.

0reactions
phryneascommented, Oct 27, 2022

Apart from that, maybe an XHR wrapper like axios might do the trick. Not sure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does HTTP 302 work? - Stack Overflow
The server returns an HTTP response with the code 302 , indicating a temporary redirection, and includes a Location: header indicating the ...
Read more >
302 Found - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily ...
Read more >
How To Fix the HTTP 302 Error (5 Methods) - Kinsta
HTTP 302 status codes are a completely normal way to redirect your website visitors. However, they should only be used when the resource...
Read more >
When to Use a 301 Redirect vs. 302 Redirect - seoClarity
A 302 redirect is a temporary redirect and directs users and search engines to the desired page for a limited amount of time...
Read more >
302 Found: What It Is and How to Fix It - Airbrake Blog
A 302 Found message is an HTTP response status code indicating that the requested resource has been temporarily moved to a different URI....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found