将文件和关联数据发布到RESTful WebService(最好是JSON) [英] Posting a File and Associated Data to a RESTful WebService preferably as JSON

查看:301
本文介绍了将文件和关联数据发布到RESTful WebService(最好是JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能会是一个愚蠢的问题,但我有一个晚上。在一个应用程序中,我正在开发RESTful API,我们希望客户端以JSON的形式发送数据。这个应用程序的一部分,需要客户上传一个文件(通常是一个图像),以及有关图像的信息。



我很难跟踪这是如何发生在一个单一的请求。是否有可能Base64文件数据到一个JSON字符串?我需要执行2个职位到服务器?我应该不使用JSON吗?

请注意,我们在后端使用Grails,这些服务由本地移动客户端(iPhone,Android等等),如果任何有所作为。

解决方案

我在这里问了一个类似的问题:

如何使用REST Web服务上传包含元数据的文件?



您基本上有三种选择:


  1. Base64对文件进行编码,代价是将数据大小增加33%左右。
  2. 首先以 multipart / form-data POST,然后返回一个ID给客户端。然后,客户端使用ID发送元数据,服务器重新关联文件和元数据。
  3. 首先发送元数据,然后将ID返回给客户端。然后客户端发送带有ID的文件,服务器将文件和元数据重新关联。


This is probably going to be a stupid question but I'm having one of those nights. In an application I am developing RESTful API and we want the client to send data as JSON. Part of this application requires the client to upload a file (usually an image) as well as information about the image.

I'm having a hard time tracking down how this happens in a single request. Is it possible to Base64 the file data into a JSON string? Am I going to need to perform 2 posts to the server? Should I not be using JSON for this?

As a side note, we're using Grails on the backend and these services are accessed by native mobile clients (iPhone, Android, etc), if any of that makes a difference.

解决方案

I asked a similar question here:

How do I upload a file with metadata using a REST web service?

You basically have three choices:

  1. Base64 encode the file, at the expense of increasing the data size by around 33%.
  2. Send the file first in a multipart/form-data POST, and return an ID to the client. The client then sends the metadata with the ID, and the server re-associates the file and the metadata.
  3. Send the metadata first, and return an ID to the client. The client then sends the file with the ID, and the server re-associates the file and the metadata.

这篇关于将文件和关联数据发布到RESTful WebService(最好是JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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