Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

json - Post binary data to a RESTful application

I'm working on a RESTful web application (Django+Piston). The POST request sends data encoded with Json to the web application. This works fine for all my text only database tables but I also have a table which stores text and binary files. What is the best way to post text and binary data to a RESTful application?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can either Base64-encode it and send it as a string in a JSON message, or you can POST or PUT the binary as a separate resource and refer to it by ID or URL in the JSON message. The latter approach is a kind of out-of-band data channel that is quite common in XML-based protocols (e.g., voice chat using XMPP).

You could even quite easily support a hybrid model, whereby:

  1. A small image is sent as {"base64":"OGZmNjJmOWNhYzFlODE0NDBjYmYzNjhjYz..."};
  2. A large image is uploaded as a reference, {"ref":"http://myserver.com/bits/E4304205-29B7-48EE-A359-74250E19EFC4"}.

To avoid the double-POST needed for externally referenced binaries, you could design some protocol that allows JSON and binary stuff to be mixed in a single transfer. But the incremental gain is unlikely to adequately reward that level of effort.

Finally, from a design perspective, stick to the simple solution until it becomes a problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...