什么是二进制数据转移到一个HTTP REST API服务的好办法? [英] What is a good way to transfer binary data to a HTTP REST API service?

查看:159
本文介绍了什么是二进制数据转移到一个HTTP REST API服务的好办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在扩展我们的HTTP REST API,允许客户端上传图片(对于这个问题的目的,假定二进制数据)。到目前为止,我们只简单地允许在我们的API参数字符串。什么是一个好办法,允许他们上载的二进制数据?难道是来请求以base64 EN codeD的形式?请问URL变得太长的Web服务器来处理?

We are extending our HTTP REST API to allow clients to upload picture (for the purpose of this question, assuming binary data). So far we have only allowed simply strings in our API parameters. What is a good way to allow them to upload binary data? Would it be to request for the base64 encoded form? Would the URL become too long for the web server to handle?

任何建议/最佳做法?

推荐答案

只是把二进制数据,是在后的身体,但用适当的Content-Type头(如图像/ JPEG) - 我认为这是最REST风格的方式。

Just send the binary data as-is in a POST body, but with the appropriate Content-Type header (e.g. image/jpeg) - I think this is the most "RESTful" way.

(一般来说,在设计REST服务时,一个经验法则,你越用HTTP协议,是不是试图覆盖像的base64它上面的一些不必要的和复杂的,就更好了。HTTP是工作最终的RESTful协议,内容类型允许在可再生能源presentational状态传输不同重新presentations)

(In general, as a rule of thumb when designing REST services, the more you work with the HTTP protocol as-is instead of trying to overlay something unnecessary and complex on top of it like base64, the better. HTTP is the ultimate RESTful protocol, and Content-Types allow for different "Representations" in "REpresentational State Transfer")

另一种可能性要记住在接受图像的URL,而不是实际的物理文件。这使得它更难独立的应用程序,例如读出用户的驱动器中的形象,而且更便于混搭式应用在图像可返回其他服务的URL。

Another possibility to keep in mind is accepting image URLs instead of actual physical files. This makes it harder for standalone apps that e.g. read the image off the user's drive, but make it easier for mashup-type apps where the image may be returned as a URL from another service.

您可以允许,当然这两个选项。

You can allow both options of course.

这篇关于什么是二进制数据转移到一个HTTP REST API服务的好办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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