Test API redirects: 301, 302, 307, 308 and the Location header

Redirects in an API look harmless until they aren't. A 301 from http:// to https:// turns your POST into a GET and drops the body. A redirect to another domain carries your Authorization header with it, or doesn't, depending on the client. A signed download URL expires because a redirect added a hop. Most API clients follow redirects automatically, which hides all of this. To test a redirect, stop following it.

See the redirect

Open the request's Settings tab and turn off Follow redirects. Now the response is the redirect, not wherever it leads:

JavaScript
pm.test('permanent redirect', () => pm.response.to.have.status(308));
pm.test('to the v2 endpoint', () => {
  pm.expect(pm.response.headers.get('Location')).to.equal(pm.variables.replaceIn('{{baseUrl}}/v2/orders'));
});

Leave it on for the requests where you only care about the final result.

Which status means what

StatusPermanent?Method and body kept?Typical use
301 Moved Permanentlyyesno, clients turn POST into GETold URLs of pages
302 Foundnono, same as 301 in practicetemporary moves, login flows
303 See Othernoalways becomes GET"here's the result" after a POST
307 Temporary Redirectnoyestemporary move of an API endpoint
308 Permanent Redirectyesyespermanent move of an API endpoint

For an API, a moved endpoint that accepts writes should use 307 or 308. A 301 on a POST endpoint is a bug report waiting to happen: clients that follow it send a GET with no body.

Things worth a test

HTTP to HTTPS. Call the http:// URL with redirects off. You want a 308 (or 301 for GET-only APIs), and ideally a Strict-Transport-Security header on the HTTPS response. Better still, write endpoints should refuse plain HTTP rather than redirect, so a client that sent a token over HTTP finds out.

Trailing slashes. /orders vs /orders/. Pick one; the other should redirect with 308, or both should work. Not a 404 on one of them.

Old versions. A retired /v1/... that redirects to /v2/... must only do so if v2 accepts the same request. Otherwise 410 Gone is the honest answer.

Open redirects. Endpoints that take a return URL (?redirect_to=, ?next=) must only redirect to your own domains:

Output
GET {{baseUrl}}/login/callback?next=https://evil.example
JavaScript
pm.test('no redirect to another site', () => {
  const location = pm.response.headers.get('Location') || '';
  pm.expect(location.startsWith('https://evil.example')).to.be.false;
});

Credentials across hosts. When a redirect goes to a different host (a CDN, a storage bucket), the Authorization header shouldn't go along. Signed storage URLs carry their own credentials in the query, and some reject a request that also has an Authorization header.

Redirect loops and chains

With Follow redirects on, a loop ends in an error after a number of hops. Each extra hop is a round trip for every client, so check that one redirect gets you to the final URL, not three. With redirects off, follow the chain by hand: send the Location of each response and see where you land.