Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to transfer binary data to a HTTP REST API service?

Tags:

rest

http

We are extending our HTTP REST API to allow clients to upload picture (for the purpose of this question, assuming binary data). So far we have only allowed simply strings in our API parameters. What is a good way to allow them to upload binary data? Would it be to request for the base64 encoded form? Would the URL become too long for the web server to handle?

Any suggestions/best practices?

like image 424
erotsppa Avatar asked Oct 02 '09 14:10

erotsppa


People also ask

Can you send binary data over HTTP?

HTTP is perfectly capable of handling binary data: images are sent over HTTP all the time, and they're binary. People upload and download files of arbitrary data types all the time with no problem.

How do I send data to REST API?

To send data to the REST API server, you must make an HTTP POST request and include the POST data in the request's body. You also need to provide the Content-Type: application/json and Content-Length request headers. Below is an example of a REST API POST request to a ReqBin REST API endpoint.

Can we send binary data in JSON?

The JSON format natively doesn't support binary data. The binary data has to be escaped so that it can be placed into a string element (i.e. zero or more Unicode chars in double quotes using backslash escapes) in JSON.

How do you send binary data to the postman?

First, set the body to "binary": Select "binary" to have Postman send binary data. Now click "Select File" in Postman to attach a file: Attach a file to upload with the "Select File" button.


1 Answers

Just send the binary data as-is in a POST body, but with the appropriate Content-Type header (e.g. image/jpeg) - I think this is the most "RESTful" way.

(In general, as a rule of thumb when designing REST services, the more you work with the HTTP protocol as-is instead of trying to overlay something unnecessary and complex on top of it like base64, the better. HTTP is the ultimate RESTful protocol, and Content-Types allow for different "Representations" in "REpresentational State Transfer")

Another possibility to keep in mind is accepting image URLs instead of actual physical files. This makes it harder for standalone apps that e.g. read the image off the user's drive, but make it easier for mashup-type apps where the image may be returned as a URL from another service.

You can allow both options of course.

like image 82
Eugene Osovetsky Avatar answered Oct 15 '22 14:10

Eugene Osovetsky