Should I encode the HTML of my web API

I am developing a web API that returns JSON as a content type, the response body may contain characters like ' , " , < and > , they are valid characters in JSON. So my question is whether I should encode HTML for my body Web API response or leave this task to an HTML client that uses my web API?

+8
json html-encode asp.net-web-api
source share
2 answers

Not; you should not.

You should avoid data if and when you combine them into a structured format.

If you return JSON, for example { "text": "Content by X &amp; Y" } , anyone who reads this JSON will see the literal text &amp; .
It will only work correctly for extremely broken clients who combine it directly into their HTML without exiting.

In short:

Never delete text unless you are going to display it.

+9
source share

What platform are you using? For example, Node.js, you can use restify for very good handling. You do not need to explicitly encode data. Therefore, please find an indispensable structure or component to help you.

0
source share

All Articles