Posting a File and Associated Data to a RESTful WebService preferably as JSON

后端 未结 11 978
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 10:45

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

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 11:22

    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%, and add processing overhead in both the server and the client for encoding/decoding.
    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.

提交回复
热议问题