困惑究竟基于REST的API是什么 [英] Confused about what exactly a REST-based API is

查看:172
本文介绍了困惑究竟基于REST的API是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解一个基于REST的API是什么。从我明白它只是一个API中写入功能的约定?所有的功能应该是是GET / POST /删除/ PUT形式?因此,例如在一个REST API函数可以是

I am trying to understand what exactly a REST-based API is. From what I understand it is just a convention for writing the functions within the API? All functions should be of either GET/POST/DELETE/PUT form? So, for example a function in a REST API could be

public string getLastName(User x)
{
    return x.lastName;
}

我主要是困惑JSON / XML如何在此发挥作用?

I am mainly confused about how JSON/XML play a role in this?

推荐答案

它不仅仅是一个约定。后面的REST API的概念是,你访问它使用HTTP动词,而这些动词已经映射的功能来执行所描述的动作。

Its more than just a convention. The concept behind a REST API is that you access it using the HTTP verbs, and that the functions those verbs have been mapped to perform the described action.

例如:

GET 将数据返回给调用者/发送

GET will return data to the caller/sender

删除会删除一条记录

和它更进一步,但很多是基于依靠HTTP提供一致性水平。例如,在RESTful服务,您可以使用接受 HTTP头请求JSON响应,或通过提供应用程序/ JSON <一个XML响应/ code>或应用程序/ XML 值,分别为。这只是一个简单的例子,它是由实施者来决定他们的API是如何工作的,但它突出于利用HTTP的重要性。

And it goes further, but a lot of it is based on relying on HTTP to provide a level of consistency. For example, in a RESTful service, you might use the Accept HTTP header to request a JSON response or an XML response by supplying the application/json or application/xml values, respectively. This is just a simple example, and it is up to the implementer to decide how their API will work, but it highlights the importance placed on leveraging HTTP.

为什么JSON / XML?

按照同样的思路,JSON和XML的使用,因为它们是wides $ P $垫和再presenting的标准方法,并通过网络传输数据。 JSON(JavaScript对象符号)是在做数据传输(尤其是GET请求),由于从JavaScript来最请求很常见的,和JS可以方便地与JSON交互,而不必做解析XML的处理时需要。在另一方面,XML提供了自己的利益,如使用模式和命名空间的能力。您可能已经知道每个优点/缺点,但是这是一个单独的讨论。主要的一点是,JSON / XML是在一个REST API的数据通信的主要方式,由于他们两人是在网络的事实上的标准。

Along the same lines, JSON and XML are used because they are widespread and standard ways of representing and transmitting data over the web. JSON (JavaScript Object Notation) is very common in doing data transfer (especially on GET requests) due to most requests coming from JavaScript, and JS can easily interact with JSON without having to do the parsing required when dealing with XML. On the other hand, XML provides its own benefits, such as the ability to use schemas and namespaces. You may already be aware of benefits/drawbacks of each, but that's a separate discussion. The main point is that JSON/XML are the primary ways of communicating data in a REST API due to both of them being the de facto standards of the web.

有很多很好的资源的更多信息,这MSDN文章可能会有所帮助:的 http://msdn.microsoft.com/en-us/library/dd203052.aspx

There are lots of good resources for more information, this MSDN article may be helpful: http://msdn.microsoft.com/en-us/library/dd203052.aspx

这篇关于困惑究竟基于REST的API是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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