什么是“Content-type:application / json; charset = utf-8“真的是什么意思? [英] What does "Content-type: application/json; charset=utf-8" really mean?

查看:7230
本文介绍了什么是“Content-type:application / json; charset = utf-8“真的是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我对我的REST服务使用JSON主体发出POST请求时,我包括 Content-type:application / json; charset = utf-8 。没有这个头,我从服务中得到一个错误。我也可以成功地使用 Content-type:application / json 而不使用; charset = utf-8

When I make a POST request with a JSON body to my REST service I include Content-type: application/json; charset=utf-8 in the message header. Without this header, I get an error from the service. I can also successfully use Content-type: application/json without the ;charset=utf-8 portion.

charset = utf-8 到底做了什么?我知道它指定字符编码,但服务工作正常没有它。

What exactly does charset=utf-8 do ? I know it specifies the character encoding but the service works fine without it. Does this encoding limit the characters that can be in the message body?

推荐答案

标题只表示内容的编码内容。不一定可以从内容本身推导出内容的类型,即,您不一定只是看内容并知道如何处理内容。这是HTTP标头,他们告诉收件人他们(据称)处理什么样的内容。

The header just denotes what the content is encoded in. It is not necessarily possible to deduce the type of the content from the content itself, i.e. you can't necessarily just look at the content and know what to do with it. That's what HTTP headers are for, they tell the recipient what kind of content they're (supposedly) dealing with.

内容类型:application / json; charset = utf-8 指定要以JSON格式的内容,以UTF-8字符编码进行编码。指定编码对于JSON有点多余,因为JSON的默认(仅?)编码是UTF-8。所以在这种情况下,接收服务器显然很高兴知道它处理JSON,并假设默认编码是UTF-8,这就是为什么它工作与或没有头。

Content-type: application/json; charset=utf-8 designates the content to be in JSON format, encoded in the UTF-8 character encoding. Designating the encoding is somewhat redundant for JSON, since the default (only?) encoding for JSON is UTF-8. So in this case the receiving server apparently is happy knowing that it's dealing with JSON and assumes that the encoding is UTF-8 by default, that's why it works with or without the header.


此编码是否限制邮件正文中可以包含的字符?

Does this encoding limit the characters that can be in the message body?

否。你可以发送任何你想要的标题和正文。但是,如果两者不匹配,您可能会得到错误的结果。如果在标题中指定内容是UTF-8编码的,但实际上是发送Latin1编码内容,接收方可能会产生垃圾数据,试图将Latin1编码数据解释为UTF-8。如果当然你指定你发送Latin1编码数据,你实际上是这样做的,那么,你只能使用256个字符,你可以在Latin1中编码。

No. You can send anything you want in the header and the body. But, if the two don't match, you may get wrong results. If you specify in the header that the content is UTF-8 encoded but you're actually sending Latin1 encoded content, the receiver may produce garbage data, trying to interpret Latin1 encoded data as UTF-8. If of course you specify that you're sending Latin1 encoded data and you're actually doing so, then yes, you're limited to the 256 characters you can encode in Latin1.

这篇关于什么是“Content-type:application / json; charset = utf-8“真的是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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