HTML – Should API Output Be HTML Encoded?

apicharacter encodinghtmlinternationalization

I am creating a RESTful JSON API to access data from our website where the content is in German.

A handful of the fields will return formatted HTML while most are single lines of text although they are highly like to include special characters.

To make it easy to use I wanted consistency throughout. As the text in the HTML fields would not be easy to encode after they have the data my first thought was to encode all fields (they can always be un-encoded later int he other fields).

Is this the best approach or should I suffix all the HTML fields e.g. description_html to imply they are already encoded or try something else?

The plan is let people use the API however they want although initially to let our partners use our data on their website.

Best Answer

Generally a web service should return raw data, and a consuming application that embeds the data in a web page would encode the data it embeds. That way other applications that might display the data in a Windows application or do some other processing on it don't have to deal with html.

If the purpose of a service is really to provide formatted text and uses html to represent the formatting, then it should be marked as such. Though you should consider how a non web-page consumer would use that data.

Related Topic