REST API Design – Is Base64 in JSON a Good Idea?

file handlingjsonrest

I am developing a Rest API, and I am asking myself:

Is it a good Idea to put base64 encoded data in Json, e.g. for file uploads? What if the base64 contains some of {,},: characters and breaks the json content?

If is not a good idea, what alternatives are widely considered to be best practices?

Best Answer

Base64 is a safe encoding for JSON.

The downside to using Base64 is that it's quite verbose. It can add significant overhead to your file upload which means longer upload and more bandwidth used. If it's possible I suggest using a binary protocol that supports JSON types. CBOR is a popular one that ElasticSearch uses for document uploads. There is also BSON.