使用 `Accept: */*` 获取属性值行为 header
Get attribute value behaviour with `Accept: */*` header
我们对 /v2/entities/{entityId}/attrs/{attrName}/value
操作行为有疑问,如果我们使用 Accept: */*
header,上下文代理会尝试 return 不受支持的格式。
curl -v orion:1026/v2/entities/Bcn-Welt/attrs/humidity/value
* Hostname was NOT found in DNS cache
* Trying 172.17.0.13...
* Connected to orion (172.17.0.13) port 1026 (#0)
> GET /v2/entities/Bcn-Welt/attrs/humidity/value HTTP/1.1
> User-Agent: curl/7.35.0
> Host: orion:1026
> Accept: */*
>
< HTTP/1.1 406 Not Acceptable
< Connection: Keep-Alive
< Content-Length: 73
< Content-Type: application/json
< Fiware-Correlator: d289fd9e-2329-11e6-88cc-0242ac11000d
< Date: Thu, 26 May 2016 10:08:41 GMT
<
* Connection #0 to host orion left intact
{"error":"NotAcceptable","description":"accepted MIME types: text/plain"}
我们需要明确接受 text/plain
格式:
curl -v orion:1026/v2/entities/Bcn-Welt/attrs/humidity/value --header "Accept: text/plain"
* Hostname was NOT found in DNS cache
* Trying 172.17.0.13...
* Connected to orion (172.17.0.13) port 1026 (#0)
> GET /v2/entities/Bcn-Welt/attrs/humidity/value HTTP/1.1
> User-Agent: curl/7.35.0
> Host: orion:1026
> Accept: text/plain
>
< HTTP/1.1 200 OK
< Connection: Keep-Alive
< Content-Length: 9
< Content-Type: text/plain
< Fiware-Correlator: 70b2a3f8-232b-11e6-a36a-0242ac11000d
< Date: Thu, 26 May 2016 10:20:16 GMT
<
* Connection #0 to host orion left intact
如果我们提供了可接受格式的列表,甚至是不支持的格式(JSON 除外),应用程序 return 就是值。
curl -v orion:1026/v2/entities/Bcn-Welt/attrs/humidity/value --header "Accept: audio/*"
* Hostname was NOT found in DNS cache
* Trying 172.17.0.13...
* Connected to orion (172.17.0.13) port 1026 (#0)
> GET /v2/entities/Bcn-Welt/attrs/humidity/value HTTP/1.1
> User-Agent: curl/7.35.0
> Host: orion:1026
> Accept: audio/*
>
< HTTP/1.1 200 OK
< Connection: Keep-Alive
< Content-Length: 9
< Content-Type: text/plain
< Fiware-Correlator: a216e33c-232b-11e6-83f3-0242ac11000d
< Date: Thu, 26 May 2016 10:21:39 GMT
<
* Connection #0 to host orion left intact
60.000000
如果我们在接受列表中提供JSON格式,则请求失败:
curl -v orion:1026/v2/entities/Bcn-Welt/attrs/humidity/value --header "Accept: text/plain" --header "Accept: application/json"
* Hostname was NOT found in DNS cache
* Trying 172.17.0.13...
* Connected to orion (172.17.0.13) port 1026 (#0)
> GET /v2/entities/Bcn-Welt/attrs/humidity/value HTTP/1.1
> User-Agent: curl/7.35.0
> Host: orion:1026
> Accept: text/plain
> Accept: application/json
>
< HTTP/1.1 406 Not Acceptable
< Connection: Keep-Alive
< Content-Length: 73
< Content-Type: application/json
< Fiware-Correlator: 375fb1a8-232c-11e6-a21e-0242ac11000d
< Date: Thu, 26 May 2016 10:25:49 GMT
<
* Connection #0 to host orion left intact
{"error":"NotAcceptable","description":"accepted MIME types: text/plain"}
个案分析:
在第一种情况下,Orion(如 1.1 版及之前的版本)首先 选择 MIME 类型, then 考虑属性类型是否适合它。使用 Accept: */*
,application/json 和 text/plain 平局,Orion 选择 application/json 作为获胜者。然后,假设属性的值是文本性质的,错误是returned.
第二种情况估计是bug。我们已经创建了 an issue at github 关于它。
第三种情况与第一种类似。 text/plain 和 application/json 之间有平局。猎户座选择了第二个。然后,当它评估属性值时,它意识到它不匹配 application/json MIME 类型和 return 错误。
事实上,在情况 1 或 3 下实施的程序并不是最好的。在 Accept
header 的所有可能选项中选择 MIME 类型之前,应 评估属性类型 。我的意思是,要实现所描述的 here:
if attribute value is an array or object and Accept header contains application/json or text/plain: return the value as a JSON with a response type of application/json or text/plain (whichever is the first in Accept header).
if attribute value is a string, number, null or boolean:
- if Accept header contains text/plain return the value as text
- else return a HTTP error "406 Not Acceptable: accepted MIME types: text/plain"
该程序将在下一个版本中在 Orion 上实现。
编辑:新程序已在 Orion 1.3.0 中实现。
我们对 /v2/entities/{entityId}/attrs/{attrName}/value
操作行为有疑问,如果我们使用 Accept: */*
header,上下文代理会尝试 return 不受支持的格式。
curl -v orion:1026/v2/entities/Bcn-Welt/attrs/humidity/value
* Hostname was NOT found in DNS cache
* Trying 172.17.0.13...
* Connected to orion (172.17.0.13) port 1026 (#0)
> GET /v2/entities/Bcn-Welt/attrs/humidity/value HTTP/1.1
> User-Agent: curl/7.35.0
> Host: orion:1026
> Accept: */*
>
< HTTP/1.1 406 Not Acceptable
< Connection: Keep-Alive
< Content-Length: 73
< Content-Type: application/json
< Fiware-Correlator: d289fd9e-2329-11e6-88cc-0242ac11000d
< Date: Thu, 26 May 2016 10:08:41 GMT
<
* Connection #0 to host orion left intact
{"error":"NotAcceptable","description":"accepted MIME types: text/plain"}
我们需要明确接受 text/plain
格式:
curl -v orion:1026/v2/entities/Bcn-Welt/attrs/humidity/value --header "Accept: text/plain"
* Hostname was NOT found in DNS cache
* Trying 172.17.0.13...
* Connected to orion (172.17.0.13) port 1026 (#0)
> GET /v2/entities/Bcn-Welt/attrs/humidity/value HTTP/1.1
> User-Agent: curl/7.35.0
> Host: orion:1026
> Accept: text/plain
>
< HTTP/1.1 200 OK
< Connection: Keep-Alive
< Content-Length: 9
< Content-Type: text/plain
< Fiware-Correlator: 70b2a3f8-232b-11e6-a36a-0242ac11000d
< Date: Thu, 26 May 2016 10:20:16 GMT
<
* Connection #0 to host orion left intact
如果我们提供了可接受格式的列表,甚至是不支持的格式(JSON 除外),应用程序 return 就是值。
curl -v orion:1026/v2/entities/Bcn-Welt/attrs/humidity/value --header "Accept: audio/*"
* Hostname was NOT found in DNS cache
* Trying 172.17.0.13...
* Connected to orion (172.17.0.13) port 1026 (#0)
> GET /v2/entities/Bcn-Welt/attrs/humidity/value HTTP/1.1
> User-Agent: curl/7.35.0
> Host: orion:1026
> Accept: audio/*
>
< HTTP/1.1 200 OK
< Connection: Keep-Alive
< Content-Length: 9
< Content-Type: text/plain
< Fiware-Correlator: a216e33c-232b-11e6-83f3-0242ac11000d
< Date: Thu, 26 May 2016 10:21:39 GMT
<
* Connection #0 to host orion left intact
60.000000
如果我们在接受列表中提供JSON格式,则请求失败:
curl -v orion:1026/v2/entities/Bcn-Welt/attrs/humidity/value --header "Accept: text/plain" --header "Accept: application/json"
* Hostname was NOT found in DNS cache
* Trying 172.17.0.13...
* Connected to orion (172.17.0.13) port 1026 (#0)
> GET /v2/entities/Bcn-Welt/attrs/humidity/value HTTP/1.1
> User-Agent: curl/7.35.0
> Host: orion:1026
> Accept: text/plain
> Accept: application/json
>
< HTTP/1.1 406 Not Acceptable
< Connection: Keep-Alive
< Content-Length: 73
< Content-Type: application/json
< Fiware-Correlator: 375fb1a8-232c-11e6-a21e-0242ac11000d
< Date: Thu, 26 May 2016 10:25:49 GMT
<
* Connection #0 to host orion left intact
{"error":"NotAcceptable","description":"accepted MIME types: text/plain"}
个案分析:
在第一种情况下,Orion(如 1.1 版及之前的版本)首先 选择 MIME 类型, then 考虑属性类型是否适合它。使用
Accept: */*
,application/json 和 text/plain 平局,Orion 选择 application/json 作为获胜者。然后,假设属性的值是文本性质的,错误是returned.第二种情况估计是bug。我们已经创建了 an issue at github 关于它。
第三种情况与第一种类似。 text/plain 和 application/json 之间有平局。猎户座选择了第二个。然后,当它评估属性值时,它意识到它不匹配 application/json MIME 类型和 return 错误。
事实上,在情况 1 或 3 下实施的程序并不是最好的。在 Accept
header 的所有可能选项中选择 MIME 类型之前,应 评估属性类型 。我的意思是,要实现所描述的 here:
if attribute value is an array or object and Accept header contains application/json or text/plain: return the value as a JSON with a response type of application/json or text/plain (whichever is the first in Accept header).
if attribute value is a string, number, null or boolean:
- if Accept header contains text/plain return the value as text
- else return a HTTP error "406 Not Acceptable: accepted MIME types: text/plain"
该程序将在下一个版本中在 Orion 上实现。
编辑:新程序已在 Orion 1.3.0 中实现。