Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
How to forward POST request to another URL and render response in Nuxt(SSR) serverMiddleware

what is the best way to forward a POST request to another URL(in Nuxt SSR) ? for example

GET /some-request - should render page
POST /some-request - should be forwarded to /api/some-request

More examples

  1. GET /some-request - should just render the page (that's working fine)
  2. GET and IF XMLHttpRequest - if ajax request, forward it to /api/some-request this also working fine with this trick
    asyncData({res, ssrContext})
     {
        if (req.headers['x-requested-with'] === 'XMLHttpRequest') {
          redirect({res, ssrContext}, route.fullPath.replace('/some-request', '/api/some-request')
          );
          return;
        }
    }
    function redirect(ctx, url) {

        ctx.ssrContext.redirected = {};
        ctx.res.writeHead(302, {
          Location: url,
        });
        ctx.res.end();

    }

This works fine with GET request

I need the exact same thing with the POST request.

I thought I can use serverMiddleware


export default (req, res, next) => {

    if ( req.headers['x-requested-with'] === 'XMLHttpRequest') {
        req.originalUrl = '/api/some-request'
    }

    next()
}

This does not work, but I am not sure what is the best way to do it. And one more thing. POST request can return either JSON or Render HTML.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!