Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send "raw" payload to Axios

How can I send a raw payload/request body to Axios?

The endpoint I'm trying to call expects the request body to just be a string which it'll scoop up and use.

If I try to just pass a string to axios.post() for the requestBody, it'll convert it to an object with no value ({ "this+is+my+message": "" }) and ends up getting parsed like this "this+is+my+message=".

I checked the documentation, but couldn't find any option that seemed to work. transformRequest seemed to be the most obvious, but it sent in the string and I sent out the string (literally d => d), but it still seemed to convert it to a valueless JSON object.

like image 569
samanime Avatar asked Jul 12 '17 20:07

samanime


People also ask

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

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.

How do I send my body through the 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 pass JSON data in Axios post?

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.


1 Answers

It turns out, if I set the Content-Type header to text/plain, it won't convert it to JSON or form data and will send it as I want.

axios.post('/my-url', 'my message text', {
  headers: { 'Content-Type': 'text/plain' }
});
like image 156
samanime Avatar answered Oct 23 '22 12:10

samanime