1

What is the deference between the following QUERY_STRING req.body, req.fields, req.params and req.body ? and When should I use each of them ?

And in Postman, there are different types of post request such as :

  1. Params
  2. Body- form-data
  3. Body x-www-form-urlencoded
  4. Body raw
  5. Body binary

What is the difference each of them and when they are possible to use each of them? Is there any relationship between the Postman body request and the QUERY_STRING ?

If I create API using Node Express and Front End using React, Which of the QUERY_STRING should I use in the Node Express API POST request?

S.Sakthybaalan
  • 499
  • 6
  • 20
  • Possible duplicate of [What is the difference between form-data, x-www-form-urlencoded and raw in the Postman Chrome application?](https://stackoverflow.com/questions/26723467/what-is-the-difference-between-form-data-x-www-form-urlencoded-and-raw-in-the-p) – user2263572 Aug 09 '18 at 02:36
  • But this is different because I also does not understand the params also. And how these data input interact with Node express QUERY_STRING. – S.Sakthybaalan Aug 09 '18 at 03:53

1 Answers1

1

1. QUERY_STRING or req.query contains the URL query parameters (after the ? in the URL)

e.g.

/profile/?query1=yogesh

so in this case req.query will be :-

{
   query1: 'yogesh'
}

2. req.params contains route parameters (in the path portion of the URL)

e.g.

'/profile/:param1' => '/profile/yogesh'

so in this case req.params will be :-

{
   param1: 'yogesh'
}

3. req.body holds parameters that are sent up from the client as part of a POST request

e.g.

POST { "name": "yogesh" }

so in this case req.body will be :-

{ 
 "name": "yogesh" 
}

which you can access by req.body.name => "yogesh"

4. req.fields there is no req.fields in express

Yogesh.Kathayat
  • 974
  • 7
  • 21
  • Thank you, So which is the best method `req.query` or `req.params` or `req.body` ? – S.Sakthybaalan Aug 09 '18 at 08:01
  • When each of the request type is suitable? – S.Sakthybaalan Aug 09 '18 at 08:30
  • It depends on your API, if you have to create something which consist of multiple type of data(string, int, array) and you don't want the data to append into the URL then you must go with post request (req.body) e.g. register api, login api, createSomeObject api. and when you have get request you can select anyone from req.query or req.params based on your preference e.g. getUser api, getBalance api – Yogesh.Kathayat Aug 09 '18 at 09:30
  • Nice Thank you so much – S.Sakthybaalan Aug 09 '18 at 09:32