REST API - 为什么使用PUT DELETE POST得到什么? [英] REST API - why use PUT DELETE POST GET?

查看:179
本文介绍了REST API - 为什么使用PUT DELETE POST得到什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我创建REST API的翻翻一些文章。
其中一些建议使用所有类型的HTTP请求:像 PUT 删除 POST GET
我们将创造例如的index.php 的写API是这样的:

So, I was looking through some articles on creating REST API's. And some of them suggest using all types of HTTP requests: like PUT DELETE POST GET. We would create for example index.php and write API this way:

$method = $_SERVER['REQUEST_METHOD'];
$request = split("/", substr(@$_SERVER['PATH_INFO'], 1));

switch ($method) {
  case 'PUT':
    ....some put action.... 
    break;
  case 'POST':
    ....some post action.... 
    break;
  case 'GET':
    ....some get action.... 
    break;
  case 'DELETE':
    ....some delete action.... 
    break;
}

确定,授予 - 我不知道很多关于Web服务(还)。
但是,是不是很容易只是接受的 JSON 的通过定期 POST对象 GET (可能包含方法名,所有参数),然后在JSON回应。我们可以很容易地通过PHP的 json_en code序列化/反序列化() json_de code()和做任何我们想做的与数据,而无需处理不同的HTTP请求方法。

OK, granted - I don't know much about web services (yet). But, wouldn't it be easier to just accept JSON object through regular POST or GET (that would contain method name and all parameters) and then respond in JSON as well. We can easily serialize/deserialize via PHP's json_encode() and json_decode() and do whatever we want with that data without having to deal with different HTTP request methods.

我缺少的东西吗?

更新1:

确定 - 通过各种API的挖掘和学习了很多有关的 XML-RPC JSON-RPC SOAP 休息后的我得出一个结论,这种类型的API是健全的。其实堆叠交换使用很多在其网站上这种做法,我认为这些人知道他们在做什么的堆叠交换API

Ok - after digging through various API's and learning a lot about XML-RPC, JSON-RPC, SOAP, REST I came to a conclusion that this type of API is sound. Actually stack exchange is pretty much using this approach on their sites and I do think that these people know what they are doing Stack Exchange API.

推荐答案

RE presentational的想法取值泰特 T 的转让(BOT)是不是在可能的最简单的方式访问的内容

The idea of REpresentational State Transfer is not about accessing data in the simplest way possible.

您使用提出请求后访问JSON,这是访问/操纵数据一个完全有效的方法。

You suggested using post requests to access JSON, which is a perfectly valid way to access/manipulate data.

REST是的有意义数据访问的方法。当你看到REST的请求时,应立即apparant什么与数据发生的事情。

REST is a methodology for meaningful access of data. When you see a request in REST, it should immediately be apparant what is happening with the data.

例如:

GET: /cars/make/chevrolet

时,可能会返回雪佛兰汽车的列表。 一个很好的REST API甚至可能包括像?输出= JSON ?输出= HTML 这将使访问,以决定什么格式的信息应在连接codeD。

is likely going to return a list of chevy cars. A good REST api might even incorporate some output options in the querystring like ?output=json or ?output=html which would allow the accessor to decide what format the information should be encoded in.

一点思考如何合理地整合数据类型为一个REST API之后,我已经得出结论,最好的方式来明确指定的数据类型是通过已有的文件扩展名,如的.js 以.json 的.html 的.xml 。如果缺少文件扩展名就会默认为任何格式是默认值(如JSON);这不是支持的文件扩展名可以返回 501未执行状态code

After a bit of thinking about how to reasonably incorporate data typing into a REST API, I've concluded that the best way to specify the type of data explicitly would be via the already existing file extension such as .js, .json, .html, or .xml. A missing file extension would default to whatever format is default (such as JSON); a file extension that's not supported could return a 501 Not Implemented status code.

另外一个例子:

POST: /cars/
{ make:chevrolet, model:malibu, colors:[red, green, blue, grey] }

时,有可能会与相关的色彩数据库创建一个新的雪佛兰Malibu的。我说的可能作为REST API并不需要直接相关的数据库结构。因此,真正的数据受到保护,这仅仅是一个屏蔽接口(认为它像存取函数的数据库结构)。

is likely going to create a new chevy malibu in the db with the associated colors. I say likely as the REST api does not need to be directly related to the database structure. It is just a masking interface so that the true data is protected (think of it like accessors and mutators for a database structure).

现在,我们需要移动到的幂等的问题。 REST通常在HTTP上实现 CRUD 。 HTTP使用 GET PUT POST 删除的请求。

Now we need to move onto the issue of idempotence. Usually REST implements CRUD over HTTP. HTTP uses GET, PUT, POST and DELETE for the requests.

有一个非常简单的实现REST的的可能的使用下面的CRUD映射:

A very simplistic implementation of REST could use the following CRUD mapping:

Create -> Post
Read   -> Get
Update -> Put
Delete -> Delete

有与本实施的一个问题:后定义为一个非幂等方法。这意味着,同样的Post方法的后续调用将导致不同服务器状态。 GET,PUT和DELETE是幂等;这意味着,称他们多次应产生相同的服务器状态。

There is an issue with this implementation: Post is defined as a non-idempotent method. This means that subsequent calls of the same Post method will result in different server states. Get, Put, and Delete, are idempotent; which means that calling them multiple times should result in an identical server state.

这意味着,一个请求,例如:

This means that a request such as:

Delete: /cars/oldest

实际上可以实施为:

could actually be implemented as:

Post: /cars/oldest?action=delete

Delete: /cars/id/123456

将导致如果你调用一次,或者如果你叫它1000次在同一台服务器的状态。

will result in the same server state if you call it once, or if you call it 1000 times.

处理去除最古老项目的一个更好的办法是要求:

A better way of handling the removal of the oldest item would be to request:

Get: /cars/oldest

和使用 ID 从得到的数据作删除要求:

and use the ID from the resulting data to make a delete request:

Delete: /cars/id/[oldest id]

这种方法的一个问题是,如果另一个 /最古老请求之间添加/汽车项目当删除印发

An issue with this method would be if another /cars item was added between when /oldest was requested and when the delete was issued.

这篇关于REST API - 为什么使用PUT DELETE POST得到什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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