Submit raw payload to Axios
How can I send a raw upload / request area to Axios?
The endpoint I'm trying to call expects the request body to be just a string that it will scoop and use.
If I try to just pass the string in axios.post()
for requestBody
, it converts it to a no-value object ( { "this+is+my+message": "" }
) and ends up getting parsed that way "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 was sent to a string and I sent the string (literally d => d
), but still seemed to convert it to a useless JSON object.
source to share
Since I ran into a similar problem, I would like to provide a further answer. I download an image file (from an element <input type="file>
) and send it to the server using axioms, but as the original body, not to package it in the request multipart/form-data
.
It seems that in the case of raw data, the axioms work best if the data is passed to ArrayBuffer
. This can be achieved, for example, using the following piece of code (ES6):
const myFile = getMyFileFrom Somewhere()
const reader = new FileReader()
reader.onload = () => {
const result = reader.result
const headers = {
'Content-Type': file.type,
'Content-Disposition': 'attachment; filename="' + file.name + '"'
}
axios.post('/upload', result, { headers: headers })
}
(using a header Content-Type
in this way is a bit non-standard, but in my case only used as a means of transporting the original filename)
Cm:
https://github.com/axios/axios#request-config - description data
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer
source to share