Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request.body vs request.params vs request.query

I have a client side JS file that has:

agent = require('superagent'); request = agent.get(url);

Then I have something like

request.get(url) 
//or
request.post(url)
request.end( function( err, results ) {
        resultCallback( err, results, callback );
    } );

On the backend Node side I have request.body and request.params and some has request.query

What are the difference between the body, params and query?

like image 477
Ming Huang Avatar asked Aug 26 '16 22:08

Ming Huang


People also ask

What is the difference between req body and REQ query?

req. query contains the query params of the request. req. body contains anything in the request body.

What is the difference between query parameter and body that you pass in API request?

Usually the content body is used for the data that is to be uploaded/downloaded to/from the server and the query parameters are used to specify the exact data requested. For example when you upload a file you specify the name, mime type, etc.

Is request parameter same as query parameter?

You can use QueryParameter to ignore form values, while you'd want to use RequestParameter if you need to read the values from a form post. RequestParameter may return more values than QueryParameter, especially if the request is a form post.

What is a request body?

A request body is data sent by the client to your API. A response body is the data your API sends to the client. Your API almost always has to send a response body. But clients don't necessarily need to send request bodies all the time.


1 Answers

req.params is route parameters, req.body is the actual body of the request, and req.query is any query parameters.

For example, if I declare this route:

router.get('/user/:id', function(req, res) {});

req.params will contain id.

If I pass a body to this route:

{
  name: 'josh'
}

This will be in req.body.

If I pass some query parameters to http://myserver.com/api/user?name="josh", req.query will be { name: 'josh' }.

Check out the Express docs.

like image 99
Josh Beam Avatar answered Oct 20 '22 17:10

Josh Beam