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

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

问题描述

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

  GET /服务/功能参数1 =值1&放大器;参数2 =值2

时正确地说,我可以用POST查询中使用它?

  POST /服务/功能{参数1:值1,参数2:值2}

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


解决方案

您无法使用 API 使用 POST GET 如果他们没有建立separetly使用这些方法来调用。就像如果你说的API

<?pre> /服务/功能参数1 =值1&放大器;参数2 =值2

通过使用 GET 方法来访问。那么,如果它没有被指定为 POST 由它的创始人方法,你可以不使用 POST 方法调用它。如果你这样做,你可能会得到不允许的405方法状态。

一般在 POST 方法,你需要将内容发送体与在内容类型为前头。 应用程序/ JSON 的JSON数据。

和该请求主体服务器端被反序列之后。所以,你需要从客户端传递序列化的数据,它是由服务开发人员决定。

但总的来说 GET 时,服务器返回一些数据到客户端并没有,而 POST服务器上的任何影响使用用于创建服务器上某些资源。所以,一般它不应该是一样的。

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

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

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

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

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?

解决方案

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

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.

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 is gets deserialized at server end. So you need to pass the serialized data from the client and it is decided by the service developer.

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.

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

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