I want to pass body parameters as shown in screen shot (in text/plain format)
I am using axios in my nodejs/express project . My reqeust format is as shown below:
var config = {
headers: {
'Content-Length': 0,
'Content-Type': 'text/plain'
}
};
const testInput = (req, res) => {
axios.post('https://api.sandbox.xyz.com/v1/order/new', { firstName: 'Marlon' }, config)
.then(function(response) {
console.log('saved successfully')
})
.catch(function(error) {
console.log(error);
});
};
For this how can I pass the body parameters appropriately?
The “content-type” header is also set to “application/json.” If you send a serialized JSON object as data, however, Axios considers it as “application/x-www-form-urlencoded” (form-encoded request body). You must manually set the header using the “headers” config option if the intended content type is JSON.
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 .
To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.
stringify method. Axios automatically transforms the data returned from the server, but with fetch() you have to call the response. json method to parse the data to a JavaScript object.
var config = {
headers: {
'Content-Length': 0,
'Content-Type': 'text/plain'
},
responseType: 'text'
};
responseType
indicates the type of data that the server will
respond with 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