我需要向 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. BooleansBoolean 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, whileoff
(the default) indicates no.
同样令人困惑的是,ABNF page实际上说:
boolean = ( "true" / "1" ) /
( "false" / "0" )
关于http - 在 HTTP header 中表示真值和假值的标准格式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58854683/