json REST - Web 服务响应 - MIME 类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4007214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:37:08  来源:igfitidea点击:

REST - web service response - mime type?

xmlweb-servicesjsonrestmime-types

提问by 001

1) Is it important to set the correct mime type for a web service response?

1) 为 Web 服务响应设置正确的 MIME 类型是否重要?

2) what is the correct mime type for a,
a) XML response?

2)
a) XML 响应的正确 mime 类型是什么?

b) JSON response?

b) JSON 响应?

application/xml 
text/xml
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json

采纳答案by Day

1) Is it important to set the correct mime type for a web service response?

1) 为 Web 服务响应设置正确的 MIME 类型是否重要?

Absolutely yes. If you are doing a true REST API then documentation of the different Media Types you return is a vital part of your API specification.

绝对没错。如果您正在执行真正的 REST API,那么您返回的不同媒体类型的文档是 API 规范的重要组成部分。

2) what is the correct mime type for a, a) XML response? b) JSON response?

2) a) XML 响应的正确 mime 类型是什么?b) JSON 响应?

For a true REST service, it depends on the details of your API and what you've defined as your content-types.

对于真正的 REST 服务,它取决于 API 的详细信息以及您定义为内容类型的内容。

As an example (taken from this excellent articlethat is worth reading in full), a Bank may want to define a Content-Type for bank accounts of application/vnd.bank.org.account+xml. Note how the MIME type "ends with +xml, and as per RFC 3023, XML processors (including XMLHttpRequest) can handle such representations as though it is XML". The same bank might also use XML to represent a bank transfer, this time using a Content-Type of application/vnd.bank.org.transer+xml

作为一个例子(取自这篇值得完整阅读的优秀文章),银行可能希望为application/vnd.bank.org.account+xml. 请注意 MIME 类型如何“以 结尾+xml,并且根据RFC 3023,XML 处理器(包括 XMLHttpRequest)可以像处理 XML 一样处理此类表示”。同一家银行也可能使用 XML 来表示银行转账,这次使用的 Content-Type 为application/vnd.bank.org.transer+xml

回答by Jacob Relkin

If you don't set a MIME type, then the resource will not be interpreted correctly by the receiver.

如果您不设置 MIME 类型,则接收方将无法正确解释资源。

Use text/xmland application/jsonrespectively.

分别使用text/xmlapplication/json

回答by Suresh Kumar

1) Is it important to set the correct mime type for a web service response?

1) 为 Web 服务响应设置正确的 MIME 类型是否重要?

Yes. However, this is really dependent on how the recipient is expected or configured to process the content. A client which is built using out-of-band information about the content may choose to ignore the content type or a client can use the content-type metadata to route the representationt to an appropriate processing module based on the content type. Content-Type enables a client make sense of the content without having to peek into the actual content. Also, since you have marked your question as related to REST, it is important to understand the self-descriptive constraint of REST and what role the media types plays to acheive this constraint. If you are interested to learn more about self-descriptiveness, read section 5.2.1 of Roy's dissertation.

是的。然而,这实际上取决于接收者如何被期望或配置来处理内容。使用有关内容的带外信息构建的客户端可以选择忽略内容类型,或者客户端可以使用内容类型元数据根据内容类型将表示路由到适当的处理模块。Content-Type 使客户端无需查看实际内容即可理解内容。此外,由于您已将问题标记为与 REST 相关,因此了解 REST 的自描述约束以及媒体类型在实现此约束方面所起的作用非常重要。如果您有兴趣了解更多关于自我描述的信息,请阅读Roy 的论文 5.2.1 节

2) what is the correct mime type for a, a) XML response?

b) JSON response?

application/xml text/xml application/json application/x-javascript text/javascript text/x-javascript text/x-jsontext/x-json

2) a) XML 响应的正确 mime 类型是什么?

b) JSON 响应?

应用程序/xml 文本/xml 应用程序/json 应用程序/x-javascript 文本/javascript 文本/x-javascript 文本/x-jsontext/x-json

All the above content types are generic and gives no more information to the client apart from saying if the content is JSON or XML. What you need is specific content-type for your application which not only tells your client about the format but also describes the semantics and how to process the content. As for the difference between application/xxx and text/xxx, RFC 3023 states the following:

以上所有内容类型都是通用的,除了说明内容是 JSON 还是 XML 之外,不会向客户端提供更多信息。您需要的是您的应用程序的特定内容类型,它不仅告诉您的客户端格式,还描述语义以及如何处理内容。对于 application/xxx 和 text/xxx 之间的区别,RFC 3023 声明如下:

If an XML document that is, the unprocessed, source XML document is readable by casual users, text/xml is preferable to application/xml. MIME user agents (and web user agents) that do not have explicit support for text/xml will treat it as text/plain, for example, by displaying the XML MIME entity as plain text. Application/xml is preferable when the XML MIME entity is unreadable by casual users. I have read in some discussions that text/xml may be deprecated in future but I am not sure of that.of that.

如果一个 XML 文档,即未处理的源 XML 文档可供临时用户阅读,则 text/xml 比 application/xml 更可取。没有明确支持 text/xml 的 MIME 用户代理(和 Web 用户代理)会将其视为 text/plain,例如,通过将 XML MIME 实体显示为纯文本。当普通用户无法读取 XML MIME 实体时,Application/xml 是首选。我在一些讨论中读到 text/xml 将来可能会被弃用,但我不确定那个。

For JSON, the correct MIME type is application/json. See SO question.

对于 JSON,正确的 MIME 类型是 application/json。请参阅SO 问题

回答by ReinaldoNS

MIME type is important for the correctly interpretation by the receiver.

MIME 类型对于接收者的正确解释很重要。

a) application/xml or text/xml

a) 应用程序/xml 或文本/xml

b) application/json

b) 应用程序/json

MIME Media Types

MIME 媒体类型

回答by inf3rno

1) Is it important to set the correct mime type for a web service response?

1) 为 Web 服务响应设置正确的 MIME 类型是否重要?

Yes, because of the uniform interface / self descriptive message constraint. The messages have to contain all information necessary to process them.

是的,因为统一接口/自描述消息约束。消息必须包含处理它们所需的所有信息。

2) what is the correct mime type for a,

2) 什么是 a 的正确 mime 类型,

It depends on what the client asks. You have to check the accept header. We usually use application/xmlby services and text/xmlby serving static files. But it does not really matter.

这取决于客户的要求。您必须检查接受标头。我们通常application/xml通过服务和text/xml通过提供静态文件来使用。但这并不重要。

If you send hyperlinks to you can use a vendor specific MIME, or you can use some linked data solution, like JSON-LD.

如果您向您发送超链接,您可以使用供应商特定的 MIME,或者您可以使用一些链接数据解决方案,如 JSON-LD。