http接受标头究竟意味着什么?

时间:2018-01-25 02:42:54

标签: http-headers

我读过this但仍感到困惑。因此,如果我收到了这样的Accept标头:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

我想解析它是这种形式:

[
    {
        mine:mine1,
        q: q1
    },
            {
        mine:mine2,
        q: q2
    }
    ....
]

结果会是什么样的?

1 个答案:

答案 0 :(得分:0)

q适用于之前的单个MIME类型。从您链接到的HTTP 1.1 RFC:

  

每个媒体范围可以后跟一个或多个accept-params,以“q”参数开头

因此q与它所遵循的媒体范围(MIME类型)相关联。

稍后,给出以下示例:

  

更详细的例子是

  Accept: text/plain; q=0.5, text/html,
          text/x-dvi; q=0.8, text/x-c
     

口头上,这将被解释为“text / html和text / x-c是   首选媒体类型,但如果它们不存在,则发送text / x-dvi   实体,如果不存在,则发送text / plain实体。“

您可以看到text/htmltext/x-c是最高质量的,q是0.8,然后是text/x-dvi用0.5。

因此,您的示例可能会被解析为

text/plain

或者,您可以在解析字符串时将[ { mine: "text/html" }, { mine: "application/xhtml+xml" }, { mine: "application/xml", q: "0.9" }, { mine: "image/webp" }, { mine: "image/apng" }, { mine: "*/*", q: "0.8" } ] 默认为q