I am wondering if there is any preference in using request.body or request.params in node.js when sending data from client to server?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Google Apps Script: testing doPost() with cURL
- How to instantiate Http service in main.ts manuall
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
相关文章
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- Is a unicode user agent legal inside an HTTP heade
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
I would say that a best practice would be that you should use params when doing a get, but use body for post, put and delete.
a sample get
a sample post
You would add validation on as well, but this has been how I have been writing all of my endpoints.
It's been over 4 years since this question was asked, however, I would still chime in to make sure somebody else stumbling upon this post has more information to understand this concept better.
req.body and req.params server two different purposes.
you would use req.body when you need to send data to a server (to store it or something), like in a "POST" request to a server. For instance, check out the following:
You are doing a "POST" request to mongodb to save a blog post. In this scenario, you would want to get the data coming in the body of the request and send it to the db. Here you would use req.body
req.params is used when you want to extract a "param" from the url. let's say you want to extract an "id" which is part of the url. For instance "id" is the number in the following url after questions stackoverflow.com/questions/18187424/
Hope, it helps.
Thanks, Kartik
You can fit more (diverse) data in the body than in the url. You can pass any string (special characters) in the body, while encoding them in the url would get you vulnerable to status 414 (Request-URI Too Long). And it's a lot easier to use the body when passing arrays and complex objects :)