是否应该在 JSON-API 中出现的链接中查询参数?响应是百分比编码的?
示例来自 jsonapi.org未编码,如:
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
}
]
}
但是,对于请求中的编码参数,还有一个注意事项:
GET /articles?include=author&fields[articles]=title,body&fields[people]=name HTTP/1.1
Accept: application/vnd.api+json
Note: The above example URI shows unencoded [ and ] characters simply for readability. In practice, these characters must be percent-encoded, per the requirements in RFC 3986.
此说明仅适用于请求吗?或者响应也应该是百分比编码的,如:
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page%5Boffset%5D=2",
"last": "http://example.com/articles?page%5Boffset%5D=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
}
]
}
最佳答案
是的,您问题中关于编码 URI 的说明仅适用于请求,不适用于响应
在 response 中返回一个 json
字符串,唯一需要转义的是双引号字符 "
GET 请求(不是响应)是一个不同的蜡球。作为 URL 中的参数传递的 GET 请求中的任何内容都必须经过 URL 编码。所以如果你有一个参数url=http://some.url.com
,参数赋值右边的url需要编码。
POST 和 PUT 请求很棘手。根据 header 中设置的内容类型,您可能需要进行编码。如果您的内容类型是 application/json
,则您不需要在 json 字符串中对任何内容进行 url 编码(不包括前面提到的 "
)。
现在,如果您声明的特定内容编码与您发送的内容不匹配(或者如果您没有明确添加一个并且它默认为某些内容),就像您发送 content-type: application/x -www-form-urlencoded
但是发送一个 json 字符串,API 服务可能接受也可能不接受它,谁知道它将如何处理其中的内容,只要 url 解码它。
关于rest - 应该对 json-api 响应中的链接进行编码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39862893/