Hassen
Hassen

Reputation: 7654

Rails API: When is it better to send Base64 encoded data instead of raw data

I'm creating an API using Ruby on Rails framework. Users can send data using POST method, passing raw data as POST parameters. But when comparing my API to others (like the one of mixpanel https://mixpanel.com/help/reference/http#base64) I see that some of them only accept padded Base64 requests.

My question is: Is it better to use padded Base64 requests and why?

Thanks.

Upvotes: 1

Views: 1759

Answers (1)

Bart Jedrocha
Bart Jedrocha

Reputation: 11570

The primary use case of base64 encoding is when you want to store or transfer data with a restricted set of characters; i.e. when you can't pass an arbitrary value in each byte.

Reference: https://stackoverflow.com/a/1682960/85125

In the case of Mixpanel, they're using a JSON api for all requests. Since JSON is a string protocol, sending certain type of data (like binary) would break the string and affect the request. Thus, they have chosen to Base64 all requests to ensure that they're transferred with a restricted set of characters acceptable by JSON.

To answer your question, it's not a matter of is it better but rather of question is it required. If you're planning on building a strict JSON api and planning on sending binary data to this API, you'll need to Base64 encode your data. Otherwise, it isn't required and I wouldn't implement it since Base64 requests are larger than Raw data requests. Hope this helps.

Upvotes: 2

Related Questions