Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios post request with json data

I am using Axios JS library for sending post json request. but I am not receiving anything at the server. Here is my code

const dt = JSON.stringify({"data":{"value":"gdfg1df2g2121dgfdg"}});
const request = axios.post(url, {dt});

I need to send post raw body in json format.

like image 507
h_h Avatar asked Mar 22 '17 17:03

h_h


People also ask

How pass JSON object in POST request Axios?

How to Send POST JSON Requests Using Axios. The POST request is used to send data to an endpoint. For example, if we have a registration page where users submit their information, this information can be sent as JSON to the endpoint we specify using a POST JSON request.

Can I send JSON in POST request?

Use The json parameter: The requests module provides a json parameter that we can use to specify JSON data in the POST method. i.e., To send JSON data, we can also use the json parameter of the requests. post() method.

Does Axios work with JSON?

By default, Axios converts Javascript data to JSON (including AJAX). The “content-type” header is also set to “application/json.” If you send a serialized JSON object as data, however, Axios considers it as “application/x-www-form-urlencoded” (form-encoded request body).

Does Axios automatically parse JSON?

When making a POST or PUT request, Axios will automatically parse the data to JSON, provided you are sending an object, and make the necessary adjustments elsewhere in the request so it can be automatically parsed once received by the server.


2 Answers

By default axios uses Json for posting data so you don't need to stringify your data. The problem could be that you're doing that. Could you try doing the post without it and check if it works? Also you don't need the curly braces to wrap your data unless that's the format of the object in your server. Otherwise could you give me information about how the body of the request looks like so I have more context? You can check that in chrome dev tools using the network tab

like image 182
Santiago Benitez Avatar answered Sep 17 '22 15:09

Santiago Benitez


You don't need to stringify your payload. Axios will do it for you when it it send a request.

const dt = { data: { value: "gdfg1df2g2121dgfdg" }};
const request = axios.post(url, dt);
like image 29
Ken Labso Avatar answered Sep 21 '22 15:09

Ken Labso