Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass text/plain content in axios POST request in nodejs

Tags:

node.js

axios

I want to pass body parameters as shown in screen shot here (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?

like image 695
Mrugesh Thaker Avatar asked Jul 26 '17 05:07

Mrugesh Thaker


People also ask

How do I specify content type in Axios?

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.

How do I pass options in Axios post?

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 .

How does Axios send post data?

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.

Does Axios Stringify JSON?

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.


1 Answers

var config = {
    headers: {
        'Content-Length': 0,
        'Content-Type': 'text/plain'
    },
   responseType: 'text'
};
  1. responseType indicates the type of data that the server will respond with
  2. options are arraybuffer , blob , document, json , text, stream
like image 149
Rajeev Radhakrishnan Avatar answered Nov 09 '22 09:11

Rajeev Radhakrishnan