Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 707
Karim Taha Avatar asked Jul 19 '18 06:07

Karim Taha


People also ask

How do you add a body to Axios?

Sending a PUT Request with Axios The simplest way to make the PUT call is to simply use the put() function of the axios instance, and supply the body of that request in the form of a JavaScript object: const res = await axios. put('/api/article/123', { title: 'Making PUT Requests with Axios', status: 'published' });

How do you send a raw data body to an Axios request in react native?

Just have your raw text within body or pass it directly within quotes as 'raw text to be sent' in place of body . The signature of the axios post is axios. post(url[, data[, config]]) , so the data is where you pass your request body.

Can I send body in Axios get?

Axios' post() function supports a data parameter that becomes the HTTP request body. On the other hand, axios. get() does not support this parameter.

How do you send a header and body in Axios post?

To send an Axios POST request with headers, you need to use the headers option. With axios. post() , the first parameter is the URL, the 2nd parameter is the request body, and the 3rd parameter is the options . For example, below is how you set the Content-Type header on an HTTP POST request.


2 Answers

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

like image 110
Ukasha Avatar answered Sep 28 '22 02:09

Ukasha


You can use the below for passing the raw text.

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

Just have your raw text within body or pass it directly within quotes as 'raw text to be sent' in place of body.

The signature of the axios post is axios.post(url[, data[, config]]), so the data is where you pass your request body.

like image 41
Madhu Bhat Avatar answered Sep 28 '22 04:09

Madhu Bhat