javascriptc#reactjspostaxios

How can I add raw data body to an axios request?


I am trying to communicate with an API from my React application using Axios. I managed to get the GET request working, but now I need a POST one.

I need the body to be raw text, as I will write an MDX query in it. Here is the part where I make the request:

axios.post(baseUrl + 'applications/' + appName + '/dataexport/plantypes' + plan,
    {
      headers: { 'Authorization': 'Basic xxxxxxxxxxxxxxxxxxx',
      'Content-Type' : 'text/plain' }
    }).then((response) => {
      this.setState({data:response.data});
      console.log(this.state.data);
    });

Here I added the content type part. But how can I add the body part?

Thank you.

Edit:

Here is a screenshot of the working Postman request Postman working request


Solution

  • How about using direct axios API?

    axios({
      method: 'post',
      url: baseUrl + 'applications/' + appName + '/dataexport/plantypes' + plan,
      headers: {}, 
      data: {
        foo: 'bar', // This is the body part
      }
    });
    

    Source: axios api