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.
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.
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 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.
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' }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With