一种方法比另一种更好吗?如果有的话,每种方法有什么独特的优点/缺点? 如果两种方法的请求都包含所有必需的数据,那么这两种方法都是RESTful API的有效实现,对吗?
例如,
app.get('/:field1/:field2/:field3' , (req, res)=>{
console.log(req.params.field1);
console.log(req.params.field2);
console.log(req.params.field3);
res.send("success");
})
与
app.post('/', (req, res)=>{
console.log(req.body.field1);
console.log(req.body.field2);
console.log(req.body.field3);
res.send("success");
}
很抱歉,如果这太对了以至于不敢相信,我仍然对REST感到困惑。
If you making fetch data from somewhere like database or API call, you should use the
get
method else you can use thepost
to store data.查看更多详细信息:
https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/
The second request suggests a
POST
request. There's many ways to skin this cat, on a high level people like to do the following:GET
to retrieve data from the server.POST
,PUT
to make changes.我强烈建议您从一些现有的API中研究API文档并从中获得启发。信息可以通过多种方式发送,但是信息出现的位置(查询参数,正文,URL的其他部分)往往具有相当特定的含义。
我还发现此资源是设计API时遵循的良好“默认规则集”的好资源:
https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md