开发者

Webservice that returns an image plus a number

If I have a webservice that returns an image, is there a standard way to also have it return some structured data (in the simplest case, an additional string or number)?

See this question for a PHP example of a webservice that returns an image. (But my question now is not specific to PHP.)

Possible solutions (mostly not very satisfying):

  1. Put the extra data in the metadata of the image.
  2. Put the extra data in 开发者_Go百科the HTTP headers.
  3. Have the webservice return a nonce URL from which the image can be fetched for a limited amount of time.
  4. Base64-encode the image and make it huge string field in a JSON or XML data structure. (Thanks to Carles and Tiago.)

How would you do it?

(I don't really want to just split the webservice into two separate calls because the webservice takes a ton of parameters and it seems wasteful to send them all twice.)


HTTP headers are a viable choice as they can be parsed easily server side and client side. Another thing you can do is setup a 302 that redirects it to a URL with the image and data in the URL (e.g ->

  1. hit http://mysite.com/bestimageever.png
  2. get 302 to http://mysite.com/realbestimage.png?supercoolinfo=42

That'd be transparent to the user and most API clients will work (since they handle redirects)


Return the image's binary data encoded in base64, and the additional field:

{ image: "mIIerhdkwje...", data: "data" }

Base64 encoding gives you an overhead of 33% (you need 4 bytes for each 3 original bytes). Depending on the size of your data it may be better to replace this with the url of the binary data and issue a second request...


Protocol buffers are another choice. Protocol buffers aren't self-describing like XML or JSON, but they are much more compact on the wire. The Google library (http://code.google.com/p/protobuf) provides C++, Java, and Python libraries, and contributors have provided libraries for a lot of other languages (http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns), including Javascript and PHP, so client writers should have an easy time working with the format.


isn't it possible to include the binary data to form the image inside the return json/xml? By this way, you could add as many fields as necessary and you could process this information in the client.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜