简体   繁体   English

HTTP“Content-Type”标头的所有可能值是什么?

[英]What are all the possible values for HTTP "Content-Type" header?

I have to validate the Content-Type header value before passing it to an HTTP request.在将Content-Type标头值传递给 HTTP 请求之前,我必须对其进行验证。

Is there a specific list for all the possible values of Content-Type ?是否有针对Content-Type所有可能值的特定列表?

Otherwise, is there a way to validate the content type before using it in an HTTP request?否则,有没有办法在 HTTP 请求中使用它之前验证内容类型?

You can find every content type here: http://www.iana.org/assignments/media-types/media-types.xhtml您可以在此处找到每种内容类型: http : //www.iana.org/assignments/media-types/media-types.xhtml

The most common type are:最常见的类型是:

  1. Type application类型应用

    application/java-archive application/EDI-X12 application/EDIFACT application/javascript application/octet-stream application/ogg application/pdf application/xhtml+xml application/x-shockwave-flash application/json application/ld+json application/xml application/zip application/x-www-form-urlencoded
  2. Type audio输入音频

    audio/mpeg audio/x-ms-wma audio/vnd.rn-realaudio audio/x-wav
  3. Type image输入图片

    image/gif image/jpeg image/png image/tiff image/vnd.microsoft.icon image/x-icon image/vnd.djvu image/svg+xml
  4. Type multipart输入多部分

    multipart/mixed multipart/alternative multipart/related (using by MHTML (HTML mail).) multipart/form-data
  5. Type text输入文字

    text/css text/csv text/html text/javascript (obsolete) text/plain text/xml
  6. Type video输入视频

    video/mpeg video/mp4 video/quicktime video/x-ms-wmv video/x-msvideo video/x-flv video/webm
  7. Type vnd :输入 vnd :

     application/vnd.android.package-archive application/vnd.oasis.opendocument.text application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.graphics application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/vnd.ms-powerpoint application/vnd.openxmlformats-officedocument.presentationml.presentation application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document application/vnd.mozilla.xul+xml

As is defined in RFC 1341 :RFC 1341 中所定义

In the Extended BNF notation of RFC 822, a Content-Type header field value is defined as follows:在 RFC 822 的扩展 BNF 表示法中,Content-Type 头字段值定义如下:

Content-Type := type "/" subtype *[";"内容类型 := 类型 "/" 子类型 *[";" parameter]范围]

type := "application" / "audio" / "image" / "message" / "multipart" / "text" / "video" / x-token类型:=“应用程序”/“音频”/“图像”/“消息”/“多部分”/“文本”/“视频”/x-token

x-token := < The two characters "X-" followed, with no intervening white space, by any token > x-token := < 后跟两个字符“X-”,中间没有空格,任何标记 >

subtype := token子类型:=令牌

parameter := attribute "=" value参数:= 属性“=”值

attribute := token属性 := 令牌

value := token / quoted-string值 := 标记 / 带引号的字符串

token := 1*令牌:= 1*

tspecials := "(" / ")" / "<" / ">" / "@" ; tspecials := "(" / ")" / "<" / ">" / "@" ; Must be in / "," / ";"必须在 / "," / ";" / ":" / "\\" / <"> ; quoted-string, / "/" / "[" / "]" / "?" / "." ; to use within / "=" ; parameter values / ":" / "\\" / <"> ; 带引号的字符串, / "/" / "[" / "]" / "?" / "." ; 使用 / "=" ; 参数值

And a list of known MIME types that can follow it (or, as Joe remarks, the IANA source ).以及可以跟随它的已知 MIME 类型列表(或者,正如 Joe 所说, IANA 来源)。

As you can see the list is way too big for you to validate against all of them.正如您所看到的,该列表太大了,您无法针对所有这些列表进行验证。 What you can do is validate against the general format and the type attribute to make sure that is correct (the set of options is small) and just assume that what follows it is correct (and of course catch any exceptions you might encounter when you put it to actual use).您可以做的是针对通用格式和type属性进行验证以确保它是正确的(选项集很小)并假设它后面的内容是正确的(当然还可以捕获您在放置时可能遇到的任何异常实际使用)。

Also note the comment above:还要注意上面的评论:

If another primary type is to be used for any reason, it must be given a name starting with "X-" to indicate its non-standard status and to avoid any potential conflict with a future official name.如果出于任何原因要使用另一种主要类型,则必须为其指定一个以“X-”开头的名称,以表明其非标准状态并避免与未来的正式名称发生任何潜在冲突。

You'll notice that a lot of HTTP requests/responses include an X- header of some sort which are self defined, keep this in mind when validating the types.您会注意到很多HTTP 请求/响应都包含某种自定义的X-标头,在验证类型时请记住这一点。

I would aim at covering a subset of possible "Content-type" values, you question seems to focus on identifying known content types.我的目标是涵盖可能的“内容类型”值的子集,您的问题似乎集中在识别已知的内容类型上。

@Jeroen RFC 1341 reference is great, but for an fairly exhaustive list IANA keeps a web page of officially registered media types here . @Jeroen RFC 1341 参考很棒,但是对于相当详尽的列表,IANA 在此处保留了官方注册媒体类型的网页。

如果您使用的是 jaxrs 或任何其他,那么在发送请求之前会有一个名为 mediatype.User 拦截器的类,并将其与此进行比较。

is there the code snippet that can accept all types有没有可以接受所有类型的代码片段

for example : like "*"例如:像“*”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM