REST API 使用 POST 而不是 GET [英] REST API using POST instead of GET

查看:51
本文介绍了REST API 使用 POST 而不是 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设一个服务提供了一些我可以这样使用的功能:

Let's assume a service offers some funcionality that I can use like this:

GET /service/function?param1=value1&param2=value2

我可以在 POST 查询中使用它是否正确?

Is it right to say that I can use it with a POST query?

POST /service/function { param1 : value1, param2 : value2 }

这两个查询是否相同?在任何情况下我都可以使用第二种变体吗,或者文档应该明确说明我可以同时使用 GET 和 POST 查询?

Are these two queries the same? Can I use the second variant in any case or the documentation should explicitly say that I can use both GET and POST queries?

推荐答案

你不能使用 API 使用 POSTGET 如果它们不是单独使用这些方法来调用的.就像你的 API 说

You can't use the API using POST or GET if they are not build to call using these methods separetly. Like if your API say

/service/function?param1=value1&param2=value2

是通过使用 GET 方法访问的.如果它的创建者没有将它指定为 POST 方法,那么你不能使用 POST 方法调用它.如果你这样做,你可能会得到 405 Method not allowed 状态.

is accessed by using GET method. Then you can not call it using POST method if it is not specified as POST method by its creator. If you do that you may got 405 Method not allowed status.

通常在 POST 方法中,您需要以指定的格式发送正文中的内容,例如在 content-type 标头中描述.application/json 用于 json 数据.

Generally in POST method you need to send the content in body with specified format which is described in content-type header for ex. application/json for json data.

然后请求正文在服务器端反序列化.所以你需要从客户端传递序列化的数据,这是由服务开发者决定的.

And after that the request body gets deserialized at server end. So you need to pass the serialized data from the client and it is decided by the service developer.

但一般来说 GET 用于服务器向客户端返回一些数据并且对服务器没有任何影响,而 POST 用于在服务器上创建一些资源.所以一般不应该一样.

But in general terms GET is used when server returns some data to the client and have not any impact on server whereas POST is used to create some resource on server. So generally it should not be same.

这篇关于REST API 使用 POST 而不是 GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆