gpt4 book ai didi

rest - application/* Content-Type 和 charset 属性

转载 作者:可可西里 更新时间:2023-11-01 15:27:03 27 4
gpt4 key购买 nike

RFC-2616 3.7.1 中的状态:

When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.

这就是为什么我通常使用例如文本/纯文本; charset=utf-8 作为 Content-Type header 。

application 类型的 MediaTypes 怎么样?

我经常看到和使用像 Content-Type: application/xml; 这样的 header 。字符集=UTF-8。 RESTeasy 2.3.7 然后强制客户端也在 Accept header 中发送字符集参数。否则它将以 406 应答。 RESTeasy 3.0.6 在这里似乎更加宽容,所以我不确定这里的最佳实践是什么。

最佳答案

RFC 2616 于 2014 年 6 月被一组 RFC 淘汰,其中包含通用 HTTP 规范的是 RFC 7213 .请使用RFC editor检查 RFC 的当前状态。

RFC 7213 明确指出(在附录 B 中):

The default charset of ISO-8859-1 for text media types has been
removed; the default is now whatever the media type definition says.

另一方面,RFC 6657 ,在预见到此类变化的同时,声明:

The default "charset" parameter value for "text/plain" is unchangedfrom [RFC2046] and remains as "US-ASCII".

因此,如果您的数据不是 ASCII(= US-ASCII),您应该继续声明 charset显式参数。

XML 规范,条款 4.3.3 , 指定:

In the absence of external character encoding information (such asMIME headers), parsed entities which are stored in an encoding otherthan UTF-8 or UTF-16 MUST begin with a text declaration [...]containing an encoding declaration

因此,对于通过 HTTP 传输的 XML,无论内容类型如何,都必须在 HTTP header 或编码声明中显式设置编码,例如<?xml encoding='UTF-8'?> .

对于 application一般来说,类型特定的规则可能适用。字符编码与大多数无关application类型,因为类型定义了自己的编码方案,包括任何嵌入字符数据的编码。

关于rest - application/* Content-Type 和 charset 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27273761/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com