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

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


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


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.


The key is to use "Content-Type": "text/plain" as mentioned by @MadhuBhat.

axios.post(path, code, { headers: { "Content-Type": "text/plain" } }).then(response => {    console.log(response);});

A thing to note if you use .NET is that a raw string to a controller will return 415 Unsupported Media Type. To get around this you need to encapsulate the raw string in hyphens like this and send it as "Content-Type": "application/json":

axios.post(path, "\"" + code + "\"", { headers: { "Content-Type": "application/json" } }).then(response => {    console.log(response);});

C# Controller:

[HttpPost]public async Task<ActionResult<string>> Post([FromBody] string code){    return Ok(code);}

https://weblog.west-wind.com/posts/2017/sep/14/accepting-raw-request-body-content-in-aspnet-core-api-controllers

You can also make a POST with query params if that helps:

.post(`/mails/users/sendVerificationMail`, null, { params: {  mail,  firstname}}).then(response => response.status).catch(err => console.warn(err));

This will POST an empty body with the two query params:

POSThttp://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&firstname=myFirstName

Source: https://stackoverflow.com/a/53501339/3850405