http - 在 HTTP header 中表示真值和假值的标准格式是什么?

标签 http http-headers

我需要向 HTTP 请求添加自定义 header ,其值的类型将为 bool 值。但是,HTTP header 值都是字符串。那么在这种情况下表示 bool 值的常见做法是什么? 0/1 , false/true , False/True , off/on , 等等?

例如。

My-Header: 1

或者一般推荐什么,即最常用的约定?

最佳答案

更新:使用 header exists 可能最有意义对比 doesn't exist约定为 implied here
HTTP 头操作 http header .EXISTS

Returns a Boolean TRUE if an instance of the specified header type exists. Following is an example:

http.req.header("CacheControl").exists



我想答案是?0对比 ?1 Structured Field Values for HTTP > 3.3.6. Booleans

Boolean values can be conveyed in Structured Fields.

The ABNF for a Boolean is:

sf-boolean = "?" boolean

boolean = "0" / "1"


bool 值用前导“?”表示字符后跟“1”
为真值或“0”为假。例如:

Example-Boolean: ?1



退房 Sec-Fetch-User :

Syntax

Sec-Fetch-User: ?0

Sec-Fetch-User: ?1

Values

The value is a Boolean Structured Header.

?0 The navigation request was triggered by a user activation.

?1 The navigation request was triggered by something other than a user activation.


但后来, Save-Data 似乎也是接受值的标准/官方“ bool 值” header "on"对比 "off" Save-Data

A boolean that indicates the user agent's preference for reduced data usage.


( https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers )

A numerical value indicating whether the client wants to opt in to reduced data usage mode. on indicates yes, while off (the default) indicates no.


同样令人困惑的是,ABNF page实际上说:

boolean = ( "true" / "1" ) /

( "false" / "0" )

关于http - 在 HTTP header 中表示真值和假值的标准格式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58854683/

相关文章:

c# - 在 .net 4.5 中使用 ClientWebSocket 时如何设置 User-Agent 和 Referer header ?

php - 授权 header 和 apache_request_headers 函数

java - 在 Java 中,如何在不生成 String 对象的情况下从 HttpServletRequest header 中提取密码?

http - 无法使用 Flutter 访问 HTTP 响应中的 Cookie

Python发送带有标题的POST

python - 如何打开网页并在python中搜索单词

scala - Scala Play 2.0获取请求 header

c++ - 无法调整缓冲区以适应数据

javascript - 使用 Angular 设置 http header

http - 什么是 HTTP 方法 PURGE?