Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post object as JSon with Axios

I have an object named entry with properties name, surname, age.

I try to use axios to send this object with post request to my REST server.

axios.post('http://host/myurl/myservice/',{data:this.ent})

However this fails with

400 Bad request

Because what is actually is being send is

 data: {data: {"name":"Jakob", "surname":"Laurence", "age":"25"} }

There is an extra data field which is not recognized on the server.

if I call

axios.post('http://host/myurl/myservice/',{
"name":this.entry.name, "surname":this.entry.surname, "age":this.entry.age 
})

Then everything works fine.

How can I post the entire object without axios generating extra data field, so that my server does not become confused?

Thanks.

P.S. All the above is going on in my Vue project (not sure if it's relevant).

like image 588
PKey Avatar asked Mar 05 '23 00:03

PKey


1 Answers

Direct pass the whole object:

axios.post('http://host/myurl/myservice/', this.ent);
like image 82
Sagar Jajoriya Avatar answered Mar 14 '23 20:03

Sagar Jajoriya