How to reuse header definitions in RAML 1.0

Andy Dufresne picture Andy Dufresne · Apr 7, 2017 · Viewed 7.1k times · Source

I have a RAML 1.0 specification where I define multiple resources. Each of the resources do have the same set of headers.

What constructs of RAML do I need use to declare the headers once and reuse them in various resource definitions?

for e.g.

/read/records:
  post:
    description: api for reading records
    queryParameters: 
      table_name: string
    headers: 
            Authorization: 
                displayName: Authorization
                description: Basic authentication base 64 encoded string
                type: string
                required: true
            Tenant-Id: 
                displayName: Tenant-Id
                description: Tenant id for multi-tenant environments
                type: string
                required: true
            Content-type: 
                displayName: Content-type
                description: either xml or json
                type: string
                required: true
/adjust/records:
  post:
    description: api for editing records
    headers: 
            Authorization: 
                displayName: Authorization
                description: Basic authentication base 64 encoded string
                type: string
                required: true
            Tenant-Id: 
                displayName: Tenant-Id
                description: Tenant id for multi-tenant environments
                type: string
                required: true
            Content-type: 
                displayName: Content-type
                description: either xml or json
                type: string
                required: true

Thanks!

Answer

Pedro picture Pedro · Apr 7, 2017

You can use traits for that:

#%RAML 1.0
traits: 
  hasHeaders: 
    headers: 
      Authorization: 
        displayName: Authorization
        description: Basic authentication base 64 encoded string
        type: string
        required: true
      Tenant-Id: 
        displayName: Tenant-Id
        description: Tenant id for multi-tenant environments
        type: string
        required: true
      Content-type: 
        displayName: Content-type
        description: either xml or json
        type: string
        required: true
/read/records:
  post:
    is: ["hasHeaders"]
    description: api for reading records
    queryParameters: 
      table_name: string
/adjust/records:
  post:
    is: ["hasHeaders"]
    description: api for editing records