且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用POST而不是GET的REST API

更新时间:2022-10-14 22:39:07

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

/服务/功能参数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.