我应该带HTML code我的Web API响应 [英] Should I HTML encode response of my Web API

查看:202
本文介绍了我应该带HTML code我的Web API响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个Web API,返回JSON作为内容类型,响应体可以包含像 < > ,他们是在JSON有效字符所以,我的问题是应该我做的HTML连接code为我的Web API响应体,或者我应该离开这个任务HTML客户,谁消耗我的Web API?

I am designing a Web API which returns JSON as the content-type, the response body could contain characters like ', ", < and >, they are valid characters in JSON. So, my question is should I do HTML encode for my Web API response body or should I leave this task to HTML client who is consuming my Web API?

推荐答案

没有;你千万不要。

您只能逃跑的数据,如果当您连接成一个结构化的格式。

You must only escape data if and when you concatenate it into a structured format.

如果您返回JSON像 {文:用X&放内容;放大器; Y} ,谁读取JSON会看到文字文本&放大器;放大器; 结果。
它只会为直接谁串连它变成自己的HTML极为破碎客户正常工作,没有逃避。

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

在短:

这篇关于我应该带HTML code我的Web API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆