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.

Is it possible to send post data to nextjs route? (now 2.0)

See original GitHub issue

I want my nextjs to recieve post form and then it can pass data to nextjs route. I don’t want to send data via query params for some reason.

server.post('/products', (req, res) => {
  // send post form with x-www-form-urlencoded
  return app.render(req, res, '/product', { payload: req.query.name })
})
Product.getInitialProps = props => {
  const { payload } = props.query
  return { payload }
}
function Product(props) {
  return <p>{props.payload}</p>
}

I can make it work when I use expressjs and run on my local server.

But when I try to deploy to now 2.0 following this https://zeit.co/guides/custom-next-js-server-to-routes/

I’m not sure how can I pass my post data form to nextjs route.

{
  "version": 2,
  "routes": [
    { "src": "/product", "method": "POST" ,"dest": "/product?payload=???? }
  ]
  "builds": [
    {
      "src": "next.config.js",
      "use": "@now/next"
    },
    { "src": "index.js", "use": "@now/node" },
    {
      "src": "static/*",
      "use": "@now/static"
    }
  ]
}

How can I send payload data that came from post form (x-www-form-urlencoded) to nextjs routes?

I try this in my index.js but it does not work

'use strict'

const express = require('express')
const helmet = require('helmet')

const app = express()

app.use(helmet())
app.use(express.urlencoded({ extended: true }))

app.post('/product', (req, res) => {
  console.log('body', req.body)
  res.status(200).send(req.body)
})

module.exports = app

I can get req.body but I cannot pass it to the nextjs route.

Is there anyway that lamdas can do something like this?

server.post('/', (req, res) => {
    console.log('payload server', req.body)
    return app.render(req, res, '/', { payload: req.body })
})

System information

  • Version of Next.js: [e.g. 8.1]

thanks for helping 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

33reactions
hvaoccommented, Sep 19, 2019

I was able to get the post form data using the package urlencoded-body-parser and parse the data from the req object inside the getInitialProps function.

import parse from 'urlencoded-body-parser'

static getInitialProps = async (ctx: NextPageContext) => {
        const { req, query } = ctx
        const data = await parse(req)
        console.log('BODY', data)
}
19reactions
amir3codecommented, Oct 10, 2019

@huv1k I wanna use Nextjs as the replacement for PHP! It should support that!

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to handle a post request in next.js? - Stack Overflow
To get POST requests working in Next.js API routes, you likely want to do 3 things. Limit the method to POST; Use JSON.parse()...
Read more >
Data Fetching: getStaticPaths - Next.js
When exporting a function called getStaticPaths from a page that uses Dynamic Routes, Next.js will statically pre-render all the paths specified by ...
Read more >
How to use Next JS to send POST requests to Salesforce ...
Show activity on this post. The console.log(data) statement in your API route simply prints to stdout — it won't actually send a response....
Read more >
The Next.js Handbook - The Valley of Code
We'll see how to provide the data to the component with getInitialProps in the next lesson. For now, add a little if (!post)...
Read more >
The Ultimate Guide to Next.js Authentication with Auth0
Learn how and where to authenticate your user in the different deployment models that exist for Next.js. This guide explores the Custom ...
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