skip to Main Content

I m not getting any req.body parameters. Its working perfectly fine while posting with postman
Code:


async function postRequest(url, data){

    const response = await axios.post(url, data, {
        headers: {
            "Content-Type": "application/json"
        }
    })
    return response.data
  }

server side:

    app.use(bodyParser.urlencoded({extended: true}))

I use this before adding any app.get functions.

Any help would be appreciated.
FYI I use reactjs

2

Answers


  1. You set the content type to json but your server is expecting application/x-www-form-urlencoded. You should set the content type to application/x-www-form-urlencoded

    async function postRequest(url, data){
    
        const response = await axios.post(url, data, {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        })
        return response.data
    }
    
    Login or Signup to reply.
  2. Try to use
    ”’
    app.use(bodyParser.json())
    ”’

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search