将文件和关联数据发布到RESTful WebService(最好是JSON) [英] Posting a File and Associated Data to a RESTful WebService preferably as JSON
问题描述
我很难跟踪这是如何发生在一个单一的请求。是否有可能Base64文件数据到一个JSON字符串?我需要执行2个职位到服务器?我应该不使用JSON吗?
请注意,我们在后端使用Grails,这些服务由本地移动客户端(iPhone,Android等等),如果任何有所作为。
我在这里问了一个类似的问题:
您基本上有三种选择:
- Base64对文件进行编码,代价是将数据大小增加33%左右。
- 首先以
multipart / form-data
POST,然后返回一个ID给客户端。然后,客户端使用ID发送元数据,服务器重新关联文件和元数据。
- 首先发送元数据,然后将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:
- Base64 encode the file, at the expense of increasing the data size by around 33%.
- 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. - 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屋!