简体   繁体   English

我的API是RESTful API吗?

[英]Is my API a RESTful API?

I have been using Phalcon to develop my web system, and have added to it an API where orders can be sent through and stock numbers can be requested. 我一直在使用Phalcon开发我的Web系统,并向其中添加了一个API,可以通过该API发送订单并可以请求库存编号。

I am new to web services, so just wandering, is my API a RESTful API? 我是Web服务的新手,所以徘徊,我的API是RESTful API吗?

  • All requests go through http://myurl.com/api . 所有请求都通过http://myurl.com/api进行
  • All requests have to put an "API-Key" in the header of the request which is used to authenticate. 所有请求都必须在用于身份验证的请求的标头中放入“ API密钥”。
  • If they want to download the stock numbers, they send a "GET" to http://myurl.com/api/stock 如果要下载库存编号,则将“ GET”发送到http://myurl.com/api/stock
  • If they want to send an order through, then send a "POST" to http://myurl.com/api/order with a "data" parameter which contains a json_encoded array of the order details. 如果他们想发送订单,则将带有“ data”参数的“ POST”发送到http://myurl.com/api/order ,该参数包含订单详细信息的json_encoded数组。

Returned data is usually a 200 status code with either success or fail (except invalid API keys which return a 401). 返回的数据通常是成功或失败的200状态代码(无效API密钥返回401除外)。

Does your service meet with the HTTP and URI standards? 您的服务是否符合HTTP和URI标准? Does it serve a standard hypermedia format? 它是否提供标准的超媒体格式? Do your clients follow the hyperlinks given by the service, instead of building the followed URIs and methods on their own? 您的客户是否遵循服务提供的超链接,而不是自己构建遵循的URI和方法? Are the hyperlinks and the response data annotated with metadata, which can be used by the clients to understand what the response mean and how to process it? 超链接和响应数据是否带有元数据注释,客户端可以使用这些元数据来理解响应的含义以及如何处理? Do your client send the information necessary to identify the client with every request? 您的客户是否发送必要的信息以识别每个请求中的客户? Would these requests work after a complete server restart? 完全重启服务器后,这些请求是否有效?

You can answer this question yourself. 您可以自己回答这个问题。 Maybe your API is a RESTful API. 也许您的API是RESTful API。
But before that you need to know somethings... 但是在此之前,您需要了解一些...

  1. What is RESTful Services/Programming..? 什么是RESTful服务/编程。
    I've got a nice and to-the-point Answer for the same 对于相同的问题,我有一个很好的要点答案

    Some points summarized here... 这里总结了一些要点...
    • URIs are the ubiquitous choice of identifier these days. 如今,URI是标识符的普遍选择。 Let URI be identifying every resource and services you wanna expose. 让URI标识您要公开的每个资源和服务。
    • HTTP methods are used. 使用HTTP方法。 Create , Retrieve , Update & Delete are handled by HTTP POST , GET , PUT & DELETE methods respectively. CreateRetrieveUpdateDelete分别由HTTP POSTGETPUTDELETE方法处理。
    • Return the result with following appropriate HTTP Response Codes 使用以下适当的HTTP响应代码返回结果

针对HTTP请求类型的HTTP代码表

  1. Does your API Offer Both JSON and XML 您的API是否同时提供JSON和XML

    Favor JSON support unless you're in a highly-standardized and regulated industry that requires XML, Schema validation and namespaces. 除非您处在需要XML,Schema验证和名称空间的高度标准化和规范的行业中,否则请支持JSON支持。 And offer both JSON and XML unless the costs are staggering. 并提供JSON和XML,除非费用惊人。 Ideally, let consumers switch between using the HTTP Accept header, or by just changing an extension from .xml to .json on the URL. 理想情况下,让使用者在使用HTTP Accept标头或通过将URL的扩展名从.xml更改为.json之间进行切换。

    Be warned though, as soon as we start talking about XML support, we start talking about schemas for validation, namespaces, etc. Unless required by your industry, avoid supporting all that complexity initially, if ever. 但是请注意,一旦我们开始谈论XML支持,我们就开始谈论验证模式,名称空间等。除非您的行业要求,否则请避免一开始就支持所有这种复杂性。 JSON is designed to be simple, terse and functional. JSON被设计为简单,简洁且实用。 Create your XML to look like that if you can. 如果可以的话,创建XML使其看起来像这样。

    In other words, make the XML that is returned more JSON-like: simple and easy to read, without the schema and namespace details present—just data and links. 换句话说,使返回的XML更像JSON:简单易读,而没有模式和名称空间详细信息-只是数据和链接。 If it ends up being more complex than this, the cost of XML will be staggering. 如果最终变得比这更复杂,那么XML的成本将是惊人的。 In my experience nobody uses the XML responses anyway over the last several years. 根据我的经验,在过去的几年中,没有人使用XML响应。 It's just too expensive to consume. 它太昂贵了,无法消费。

    To read more on How a RESTful API should be 阅读有关RESTful API应该如何的更多信息

  2. Please read the valuable comments of this answer as well. 请阅读该答案的宝贵意见。 It'll really help. 真的有帮助。 (Also you can read the answer for if you want some more details) (如果您需要更多详细信息,也可以阅读答案)


Now, I think you can decide for yourself whether your API is a RESTful API 现在,我认为您可以自行决定您的API是否为RESTful API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM