4

I have a simple Axios POST request:

 const data = JSON.stringify({
    to: receiver,
    from: sender,
    body: message
  });

  axios.post(window.location.origin + '/sms/outgoing', data)

My issue is that my api reads the request body as this:

{ '{"to":"12345","from":"54321","body":"message"}': '' }

when I want it to be this:

{"to":"12345","from":"54321","body":"message"}

Where am I going wrong?

achalk
  • 3,229
  • 3
  • 17
  • 37

2 Answers2

1

JSON.stringify method is not need..

  const data = {
      to: receiver,
      from: sender,
      body: message
    };

  axios.post(window.location.origin + '/sms/outgoing', data)

but you need JSON.stringify method to follow this backend api call

  app.route(window.location.origin + '/sms/outgoing',(req,res)=>{
    let data = JSON.parse(req.body)
    console.log(data) //get {"to":"12345","from":"54321","body":"message"}
 })
SM Chinna
  • 341
  • 2
  • 7
-1

If you want to parse the JSON object from a JSON string, just use the JSON.parse function

const object = JSON.parse({
  someJSONString: true,
})
Felix Fong
  • 969
  • 1
  • 8
  • 21