Search code examples
javascriptreactjsapipython-requestsbody-parser

How to fix javascript not sending any req.body to server


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


Solution

  • 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
    }