23

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.

mruanova
  • 6,351
  • 6
  • 37
  • 55
Azima
  • 3,835
  • 15
  • 49
  • 95
  • If you are sure that you are making the request by POST, maybe your problem is bodyParser. Please ensure that your express instance is using it. – jose920405 Nov 23 '20 at 15:20

5 Answers5

27

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.

pidizzle
  • 720
  • 6
  • 12
  • 23
    GET Bodies are no longer banished from HTTP requests, and suggesting to change it to a POST does not help those of us using an external API that we have no control over. – user37309 Oct 27 '20 at 02:00
  • My answer was not for people hitting an external api that they don't control, it was for the question you see above. Also I said you "should not" have a GET not that you "could not" – pidizzle Oct 27 '20 at 16:58
  • 6
    Perhaps, but that is more down to opinion now. Since the standards now don't explicitly exclude GET bodies any more I'd say that 'should not' is a very personal opinion. This answer also comes up in Google when you search for answers about HTTP GET bodies so it is rather unhelpful in that sense. – user37309 Oct 28 '20 at 05:03
  • 3
    @user37309 while they may no longer be _"banished"_, they are not supported in browsers at all. – Phil Mar 15 '22 at 01:14
10

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 */
MaieonBrix
  • 1,584
  • 2
  • 14
  • 25
1

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.

VirxEC
  • 998
  • 10
  • 22
Abhishek
  • 31
  • 1
0

Because get requests... dont generally have bodies. They are esentially what you type into a url bar. So instead of a body, you would want to use parameters. If you wished to use them individually you use querystrings.

url/posts?post=post123&comment=comment321

in the below the request made to url/posts/post123/comment321 => will fill the two below parameters.

you must use post but comment is not required.

router.get("/posts/:post/:comment",async(req,res){
    const postPermlink = req.params.post
    const commentPermlink = req.params.comment ?? null
    const postDbEntry = await Posts.findOne({permLink: postPermlink})
}
josh hoffer
  • 126
  • 1
  • 7
-1

Try this

this.axios('realties', { params: this.filter })
Pablo Papalardo
  • 1,224
  • 11
  • 9