发布二进制数据RESTful应用程序 [英] Post binary data to a RESTful application
问题描述
我工作的一个RESTful Web应用程序(Django的+活塞)。 POST请求发送数据连接codeD使用JSON的Web应用程序。这工作正常,所有我的文字只有数据库表,但我也有存储文本和二进制文件的表。什么是张贴文本和二进制数据到一个RESTful应用程序的最佳方式?
I'm working on a RESTful web application (Django+Piston). The POST request sends data encoded with Json to the web application. This works fine for all my text only database tables but I also have a table which stores text and binary files. What is the best way to post text and binary data to a RESTful application?
推荐答案
您既可以Base64编码恩code,并把它作为一个JSON消息的字符串,也可以邮寄或把二进制作为一个单独的资源和在JSON消息指其通过ID或URL。后一种方法是一种从带外数据信道的那是相当常见的基于XML的协议(例如,使用XMPP语音聊天)。
You can either Base64-encode it and send it as a string in a JSON message, or you can POST or PUT the binary as a separate resource and refer to it by ID or URL in the JSON message. The latter approach is a kind of out-of-band data channel that is quite common in XML-based protocols (e.g., voice chat using XMPP).
您甚至可以很容易地支持混合模式,即:
You could even quite easily support a hybrid model, whereby:
- 的小图像被作为
{BASE64:OGZmNjJmOWNhYzFlODE0NDBjYmYzNjhjYz ...}
; - 一个大的图像上传作为参考,
{裁判:http://myserver.com/bits/E4304205-29B7-48EE-A359-74250E19EFC4}
。
- A small image is sent as
{"base64":"OGZmNjJmOWNhYzFlODE0NDBjYmYzNjhjYz..."}
; - A large image is uploaded as a reference,
{"ref":"http://myserver.com/bits/E4304205-29B7-48EE-A359-74250E19EFC4"}
.
要避免需要外部引用二进制双POST,你可以设计一些协议,允许JSON和二进制的东西,在一个单一的转移到混合。但增量收益是不可能充分奖励的努力,这一水平。
To avoid the double-POST needed for externally referenced binaries, you could design some protocol that allows JSON and binary stuff to be mixed in a single transfer. But the incremental gain is unlikely to adequately reward that level of effort.
最后,从设计的角度来看,坚持简单的解决方案,直到它成为一个问题。
Finally, from a design perspective, stick to the simple solution until it becomes a problem.
这篇关于发布二进制数据RESTful应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!