REST API:请求体JSON或纯POST数据? [英] REST API: Request body as JSON or plain POST data?

查看:2070
本文介绍了REST API:请求体JSON或纯POST数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设一个REST API。所有GET方法目前使用JSON作为响应格式。什么在POST和PUT操作的最佳做​​法?使用JSON请求体或纯POST?我找不到这件事什么。

I'm currently building a REST API. All GET methods currently use JSON as response format. Whats the best practice on POST and PUT operations? Use JSON in the request body or plain POST? I cannot find anything about this matter.

我看到Twitter使用POST比如:
<一href=\"https://dev.twitter.com/docs/api/1/post/direct_messages/new\">https://dev.twitter.com/docs/api/1/post/direct_messages/new

I see Twitter uses POST for instance: https://dev.twitter.com/docs/api/1/post/direct_messages/new

什么是使用JSON格式的好处是什么?我从GitHub得到的API控制器(这是成功的一半)预计JSON。真的不知道为什么我会选择使用。

What are the benefits of using a JSON format? The API controller (which is half done) I got from github expects JSON. Really wondering why I would choose using that.

推荐答案

POST,PUT,GET都是HTTP动词和不这样做,在他们自己的,并表示用于传输数据的格式,这样的不存在POST格式的。这意味着,你可以连接code在你选择的任何方式的数据。

POST, PUT, GET are all HTTP verbs and do not, in and of themselves, indicate a format for transferring the data, so there is no POST format. That means that you can encode the data in any way you choose.

现在,你决定去应的真正的更多的是如何你的API通常使用的不管是什么格式。如果将主要守备形式从Web浏览器提交,然后用表单字段编码可能是最合理的事,因为它使这种互动更容易为客户端。

Now, what format you decide to go with should really be more a matter of how your API will generally be used. If it will be primarily fielding form submits from a web browser, then using a form fields encoding is likely the most reasonable thing to do since it makes that interaction easier for the client.

在另一方面,如果你主要是将要接收JSON数据来自AJAX调用,然后接收JSON格式可能是有意义的。如果将两者都做,没有任何原因,你不能接受数据的两个的格式。

On the other hand, if you are primarily going to be receiving JSON data from AJAX calls, then receiving a JSON format may make sense. If you will do both, there isn't any reason you can't accept data in both formats.

的其他方面要考虑的是,你将被来回传递的数据结构的复杂性。形式编码(类似于查询字符串编码以及)是一个key-value结构,而JSON(或XML)允许更丰富的数据结构。

The other aspect to consider is the complexity of the data structures you will be passing back and forth. Form encoding (similar to query-string encoding as well) is a key-value structure, while JSON (or XML) allows for a much richer data structure.

在最后,去什么是最简单的为您在服务器端,以及您在客户端(因为我假定你也将写有问题的API的主要客户端消费者)。简单是的总是的preferred了复杂性,直到你可以明确地表明,更多的复杂性给你一个衡量的好处。

In the end, go with whatever is simplest for both you on the server side, as well as you on the client side (since I assume you will also be writing the primary client consumer of the API in question). Simplicity is always preferred over complexity until you can definitively show that more complexity gives you a measurable benefit.

此外,过去的事情,我会提的是,REST不仅仅是干净的网址或使用HTTP动词正确。这些方面其实只是锦上添花。一个REST架构背后的核心思想是,的 是超文本应用程序状态 的发动机。通过在服务器响应简单以下URL,良好的客户端可以了解所有可用的操作,也不需要知道任何事情比基本URL多。一切可以从被发现。夫妇,与明确的内容类型,你有一个世界里,很多客户都可以用大量的服务器进行通信,都讲同一种语言,而客户机并不需要知道的任何的有关服务器(反之亦然),比基本URL和内容类型等。这是REST是怎么一回事。

Also, the last thing I will mention is that REST isn't just about clean URLs or using HTTP verbs correctly. Those aspects are really just icing on the cake. The core idea behind a REST architecture is that Hypertext is the engine of application state. By simply following URLs in the server responses, a good client can learn about all of the available actions and doesn't need to know anything more than the base URL. Everything else can be discovered from that. Couple that with well defined content types and you have a world where lots of clients can communicate with lots of servers, all speaking the same "language", and the clients don't need to know anything about the servers (or vice-versa) other than the base URL and the content types. That's what REST is all about.

这篇关于REST API:请求体JSON或纯POST数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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