应该对json-api响应中的链接进行编码吗?

问题描述:

如果查询参数出现在JSON-API响应中的链接是百分比编码的?应该对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 

注:上面的示例URI显示未编码[和]字符只是为了便于阅读。实际上,这些字符必须按照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在你的问题的说明仅适用于请求,而不是响应

随着内返回的json串响应,逃避唯一需要的是双引号字符"

GET请求(未回答)是蜡不同的球。作为URL中的参数传递的GET请求中的任何内容都必须进行URL编码。所以如果你有一个参数url=http://some.url.com,参数赋值右边的url需要编码。

POST和PUT请求很棘手。根据标题中设置的内容类型,您可能需要编码。如果您的内容类型为application/json,则不需要在您的json字符串中进行url编码(不包括前面提到的")。

现在,如果您指定的内容编码与您发送的内容不匹配(或者您没有明确添加内容并且默认为某个内容),就像发送content-type: application/x-www-form-urlencoded但发送了json字符串一样,API服务可能会或可能不会接受它,并且知道它会如何处理内部的内容,就像解码它的url一样。