如何在 RAML 中定义通用响应 header 参数

How to define a generic responce header parameter in RAML

我需要定义一个响应 header 参数,该参数对于 RAML API.

中的所有 2xx 响应代码都是通用的

我只找到了一种为每个 HTTP 代码定义参数的方法,如下所示:

 responses:
  200:
   headers:
    X-Transaction-Id:
      type: string

但我需要做这样的事情:

 responses:
      [200-300):
       headers:
        X-Transaction-Id:
          type: string

有什么帮助吗?

你为什么不尝试将其实现为库中的资源类型。

#%RAML 1.0 Library
usage: |
  All common response types...
types:
  common-response:
    type: object
    properties: 
      key1: string
      key2: string

resourceTypes: 
  common_response_types:
    description: Common response types for API
    get?:
      responses:
        200:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
        201:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
        300:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
    post?:
      responses:
        200:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
        201:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
        300:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
    patch?:
      responses:
        200:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
        201:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
        300:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
    delete?:
      responses:
        200:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
        201:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml
        300:
          body:
            application/json:
              type: common-response
              example: !include examples/example-response.raml

您可以使用它来调用它。

uses:
  common_responses: libraries/common-responses.raml

/getuser:
  type: common_responses.common_response_types

不能那样做。您需要分别声明每个状态码。