Axios post request with json data

I am using the Axios JS library to send a post json request. but I get nothing on the server. Here is my code

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

I need to send a raw body message in json format .

+10
source share
3 answers

By default, axios uses Json to send data, so you do not need to strict your data. The problem may be that you are doing this. Could you try to make a message without it and check if it works? In addition, you do not need curly braces to transfer your data, except when the format of the object is on your server. Otherwise, could you give me information on what the request body looks like, so do I have more context? You can check that in chrome dev tools using network tab

+6
source

Axios for mail request with json as body:

  static async postService(path, data = {}) {
    const requestUrl = HttpRequest._getRequestUrl(path);

    try {
      const ret = await axios.post(requestUrl, JSON.stringify(data));
      console.log('Request result ', ret);
    } catch (error) {
      console.error('Request error: ${error.message}');
    }
  }
0
source

You do not need to fix your payload. Axios will do this for you when it sends the request.

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

Source: https://habr.com/ru/post/1015945/


All Articles