如何在 RAML 中正确定义 Accept-Encoding: gzip 头部?

huangapple go评论54阅读模式
英文:

How to correctly define Accept-Encoding: gzip header in RAML?

问题

我需要描述一个符合RAML 1.0规范的REST API,该API始终以gzipped响应返回。

API RAML规范必须指示API需要始终在请求中发送头部Accept-Encoding
此外,Accept-Encoding的值必须是'*'、'gzip'或在其他可接受编码中包含'gzip',例如'compress, gzip'或'compress;q=0.5, gzip;q=1.0'等。

作为第一次迭代,我已经这样定义了必需的头部:

headers:
  Accept-Encoding:
    type: string
    required: true
    example: 'gzip'

但这当然不表示API只支持'gzip'编码。请帮助定义更详细的'Accept-Encoding'头部的RAML规范。

英文:

I need to describe a REST API that always returns gzipped response in terms of RAML 1.0 specification.

The API RAML specification must indicate that the API requires the header Accept-Encoding be always sent in the request.
Furthermore, the Accept-Encoding value must be either '*' or 'gzip' or have 'gzip' among other acceptable encodings specified e.g. 'compress, gzip' or 'compress;q=0.5, gzip;q=1.0'.

As a first iteration, I've defined the mandatory header like this:

headers:    
  Accept-Encoding:
    type: string
    required: true
    example: 'gzip'

But this, of course, does not indicate that only 'gzip' encoding is supported by the API.
Pls help with defining more detail RAML specification for the 'Accept-Encoding' header.

答案1

得分: 1

RAML 不具备表达性来验证具有特定规则的值。您可以使用正则表达式设置一个 pattern 来限制该值:

headers:    
  Accept-Encoding:
    type: string
    required: true
    example: 'gzip'
    pattern: ^(\*|((.+,)?\s*gzip\s*((,|;).+)?))$

这个模式并不能保证该值是有效的列表,但它验证了它要么只是 *gzip,要么被逗号包围。您可以改进正则表达式,以确保两侧都是逗号分隔的列表,然而,这种验证超出了 RAML 的预期范围,应属于应用程序代码。

英文:

RAML doesn't has the expressiveness to validate the values with specific rules. You can set a pattern with a regular expression to restrict the value:

headers:    
  Accept-Encoding:
    type: string
    required: true
    example: 'gzip'
    pattern: ^(\*|((.+,)?\s*gzip\s*((,|;).+)?))$

This pattern doesn't guarantee the value will be a valid list but it verifies that it is either only * or gzip standalone or surrounded in either side by commas. You could improve the regular expression to ensure that both sides are comma separated lists, however that kind of validation is outside of what is expected of RAML and belongs in the application code.

huangapple
  • 本文由 发表于 2023年5月25日 07:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定