简体   繁体   English

REST API:请求主体为JSON还是普通POST数据?

[英]REST API: Request body as JSON or plain POST data?

I'm currently building a REST API. 我目前正在构建REST API。 All GET methods currently use JSON as response format. 所有GET方法目前都使用JSON作为响应格式。 Whats the best practice on POST and PUT operations? 什么是POST和PUT操作的最佳实践? Use JSON in the request body or plain POST? 在请求正文或普通POST中使用JSON? I cannot find anything about this matter. 我找不到任何关于此事的内容。

I see Twitter uses POST for instance: https://dev.twitter.com/docs/api/1/post/direct_messages/new 我看到Twitter使用POST,例如: https//dev.twitter.com/docs/api/1/post/direct_messages/new

What are the benefits of using a JSON format? 使用JSON格式有什么好处? The API controller (which is half done) I got from github expects JSON. 我从github得到的API控制器(已完成一半)需要JSON。 Really wondering why I would choose using that. 真想知道为什么我会选择使用它。

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 . POST,PUT,GET都是HTTP动词,它们本身并不表示传输数据的格式,因此没有POST格式 That means that you can encode the data in any way you choose. 这意味着您可以以您选择的任何方式对数据进行编码。

Now, what format you decide to go with should really be more a matter of how your API will generally be used. 现在,你决定什么格式去真正应该更多的是如何你的API通常使用的问题。 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. 如果它主要是从Web浏览器提交表单提交,那么使用表单字段编码可能是最合理的事情,因为它使客户端的交互更容易。

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. 另一方面,如果您主要要从AJAX调用接收JSON数据,那么接收JSON格式可能有意义。 If you will do both, there isn't any reason you can't accept data in both formats. 如果您同时执行这两项操作,则没有任何理由您无法接受这两种格式的数据。

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. 表单编码(类似于查询字符串编码)是键值结构,而JSON(或XML)允许更丰富的数据结构。

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). 最后,请选择服务器端的最简单的内容,以及客户端上的最简单(因为我假设您也将编写相关API的主要客户端使用者)。 Simplicity is always preferred over complexity until you can definitively show that more complexity gives you a measurable benefit. 简单性总是优先于复杂性,直到您可以明确地表明更复杂性为您带来可衡量的好处。

Also, the last thing I will mention is that REST isn't just about clean URLs or using HTTP verbs correctly. 另外,我要提到的最后一件事是REST不只是关于干净的URL或正确使用HTTP动词。 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 . REST架构背后的核心思想是超文本是应用程序状态的引擎 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. 通过简单地跟踪服务器响应中的URL,一个好的客户端可以了解所有可用的操作,并且不需要知道除基本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. 结合具有明确定义的内容类型,并且您拥有许多客户可以与许多服务器通信的世界,所有服务器都使用相同的“语言”,并且客户端不需要了解有关服务器的任何信息(反之亦然)基本URL和内容类型。 That's what REST is all about. 这就是REST的全部意义所在。

It depends on the data you want to exchange. 这取决于您想要交换的数据。 If it is a complex structure you need to send it in a structured way (eg XML or JSON). 如果它是一个复杂的结构,您需要以结构化的方式发送它(例如XML或JSON)。 In java web applications json is more lightweight, so it's preferable over XML. 在java web应用程序中,json更轻量级,因此它优于XML。

If you want to send a few fields from a form "application/x-www-urlformencoded" type could be used as well. 如果您想从“application / x-www-urlformencoded”表单中发送一些字段,也可以使用。

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

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