express: what is the difference between req.query and req.body

后端 未结 2 1611
梦如初夏
梦如初夏 2021-02-06 13:31

I want to know what is the difference between req.query and req.body?

below is a piece of code where req.query is used. what happens if i use req.body

2条回答
  •  庸人自扰
    2021-02-06 13:43

    req.query contains the query params of the request.

    For example in sample.com?foo=bar, req.query would be {foo:"bar"}

    req.body contains anything in the request body. Typically this is used on PUT and POST requests.

    For example a POST to sample.com with the body of {"foo":"bar"} and a header of type application/json, req.body would contain {foo: "bar"}

    So to answer your question, if you were to use req.body instead of req.query, it would most likely not find anything in the body, and therefore not be able to validate the jwt.

    Hope this helps.

提交回复
热议问题