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.

redirect is routing POST requests to GET

See original GitHub issue
const Koa = require('koa')
const Router = require('koa-router')

const app = new Koa()
const router = new Router()

router.post('/newRoute/subPath', (ctx) => {
    ctx.body = 'Hello, from POST';
});

router.get('/newRoute/subPath', (ctx) => {
    ctx.body = 'Hello, from GET';
});

router.put('/newRoute/subPath', (ctx) => {
    ctx.body = 'Hello, from PUT';
});

router.delete('/newRoute/subPath', (ctx) => {
    ctx.body = 'Hello, from DELETE';
});

router.all(/^\/oldRoute\//, (ctx) => {
    const newUrl = ctx.url.replace(/^\/oldRoute\/(.*)/, '/newRoute/$1');
    console.log(`-- redirecting request -- ${ctx.url} -> ${newUrl}`);

    ctx.redirect(newUrl);
});

app
    .use(router.routes())
    .use(router.allowedMethods());

app.listen(3002, () => console.log('Koa app listening on 3002'));

Calling localhost:3002/oldRoute/subPath with GET, PUT, and DELETE methods correctly redirect to the new url with the same method. However, calling that url with a POST method redirects to the new url but ends up in the GET method handler when it should end up in the POST handler for the new url.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

5reactions
edwmurphcommented, Sep 13, 2017

That’s right. The problem was resolved by setting ctx.status = 308 before redirecting. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to redirect to a post route - laravel - Stack Overflow
i want to be redirected to a post route using the redirect instead of a get. My code below public function resolve_booking(Request $request){ ......
Read more >
Redirections in HTTP - MDN Web Docs - Mozilla
In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that ...
Read more >
Routing - Express.js
For example, the following handler is executed for requests to the route “/secret” whether using GET, POST, PUT, DELETE, or any other HTTP...
Read more >
HTTP Redirects - The PHP Framework For Web Artisans
Redirect responses are instances of the Illuminate\Http\RedirectResponse class, ... Since this feature utilizes the session, make sure the route calling the ...
Read more >
next.config.js: Redirects
For example, if the browser made a request to POST /v1/users which returned status code 302 with location /v2/users , the subsequent request...
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