Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

body data not sent in axios request

I am trying to send data through axios request to my backend script, but the body looks empty.

Here's a request sent from front-end:

axios.request({
  method: 'GET',
  url: `http://localhost:4444/next/api`,
  headers: {
    'Authorization': token
  },
  data: {
    next_swastik: 'lets add something here'
  },

}).then((res)=>{  
  console.log("api call sucessfull",res);

}).catch((err)=>{
  console.log("api call unsucessfull",err);

  this.props.toggleLoading(false);
})

Here's a back-end:

app.get('/next/api', verifyToken, function(req, res) {
console.log(req.body);

})

But I am getting {} empty body. I am getting headers and other data but not data.

like image 727
Azima Avatar asked Sep 28 '18 18:09

Azima


People also ask

How do I send my body through the Axios?

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 do I send body parameters in POST request Axios?

First we're passing the url of the service endpoint. Second we're passing object params which we created above and lastly we will pass headers to the post request. To pass raw data body content-type should be application/json.

How pass JSON data in Axios post?

To post request with JSON data with Axios, we call axios. post with the JSON payload as the 2nd argument. const dt = { data: { value: "abc" } }; const request = axios. post(url, dt);

Can we send JSON in GET request Axios?

POST JSON with Axios 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.


3 Answers

GET requests should not have a body.

Change the method from 'GET' to 'POST'

Like so:

axios.request({
  method: 'POST',
  url: `http://localhost:4444/next/api`,
  headers: {
    'Authorization': token
  },
  data: {
    next_swastik: 'lets add something here'
  },

})

and change your api to expect a post

app.post('/next/api', verifyToken, function(req, res) {
console.log(req.body);
});

or

Change the data property to params

axios.request({
  method: 'GET',
  url: `http://localhost:4444/next/api`,
  headers: {
    'Authorization': token
  },
  params: {
    next_swastik: 'lets add something here'
  },

})

and change the api to log out the params

app.get('/next/api', verifyToken, function(req, res) {
console.log(req.params);
});

and like @MaieonBrix said, make sure that your headers contain the content type that you are sending.

like image 177
pidizzle Avatar answered Oct 21 '22 11:10

pidizzle


It looks like you only have two points left to make it work :

  • one : the http method should be set to POST instead of GET since you want to send something.

  • two : you can then add the http header (like what you did with the authorization header) Content-Type: 'application/json`

On the back-end don't forget to use some kind of body parser utility package like this one : body-parser and set it up with your app.

I suppose your server is using express, here is how you will do it with express :

const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const jsonParser = bodyParser.json();

app.use(jsonParser); // use it globally
app.get('your_route', jsonParser, otherMiddleware, (req, res) => ...); // use it for specific routes

/* ... rest of your code */
like image 10
MaieonBrix Avatar answered Oct 21 '22 12:10

MaieonBrix


If you get an error that "bodyParser is deprecated", try this -

app.use(express.json()); //To parse JSON bodies (Applicable for Express 4.16+)

And use "post" method, if you want to get data from body of the HTTP request.

like image 1
Abhishek Avatar answered Oct 21 '22 10:10

Abhishek