我正在设计一个REST API,该API可以响应多种格式,其中之一是纯文本格式,可以配置以显示或隐藏响应中的某些部分(例如:章节标题或脚注)。常规的处理方式是通过URL查询参数来完成,既可以指示所需的响应类型,也可以配置选项,例如:
http://api.example.com/foo-book/ch1/?format=text&headings=false&footnotes=true
然而,一种更优雅的RESTful方式来指示所需的响应类型(而不是使用format=text
URL查询参数)是使用Accept
头部,例如:
Accept: text/plain; charset=utf-8
现在,除了URL之外,媒体类型可以根据RFC 2046中的规定接受参数,这些参数可以在广泛使用的text/html; charset=utf-8
和Accept
头文件中看到,例如:audio/*; q=0.2
。还可以发现特定厂商创建的MIME类型可以定义它们自己的参数,例如:
application/vnd.example-com.foo+json; version=1.0
application/vnd.example-info.bar+xml; version=2.0
那么对于像 text/html
或者 application/json
这些之前注册过的MIME类型,可以包含自定义参数来满足应用程序的需求吗?例如:
Accept: text/plain; charset=utf-8; headings=false; footnotes=true
这似乎是一个优雅的RESTful解决方案,但它也似乎违反了一些规定。 RFC 2046 §1 表示:
Parameters are modifiers of the media subtype, and as such do not
fundamentally affect the nature of the content. The set of
meaningful parameters depends on the media type and subtype. Most
parameters are associated with a single specific subtype. However, a
given top-level media type may define parameters which are applicable
to any subtype of that type. Parameters may be required by their
defining media type or subtype or they may be optional. MIME
implementations must also ignore any parameters whose names they do
not recognize.
请提供需要翻译的具体内容。MIME implementations must also ignore any parameters whose names they do not recognize.
这是否意味着,如果客户端识别出 text/plain
媒体类型的 footnotes=true
参数,则该客户端将不符合规范?
text/plain
的line-length
参数:如果存在如http://api.example.com/foo?line-length=74
的 URL,并且我使用Accept: text/html
请求它,那么查询参数应该被忽略吗?还是响应应包含一个Content-Location: http://api.example.com/foo
头以及相同的 URL 在<link rel='canonical' href='http://api.example.com/foo'/>
(又名<atom:link rel='self' href='http://api.example.com/foo'/>
)中,以表明在提供响应时未考虑该参数? - Weston Ruterfile_get_contents()
拉取数据,对于这些情况你无法提供自己的请求头。 - Weston Ruter