question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to reference headers in response

See original GitHub issue

I am trying to figure out a way to reuse headers in response. The documentation and google isn’t helping much. I am wondering if this is even possible to include headers inside response using $ref.

My response is defined as below. In the successful response my Api returns pagination information in headers. This pagination headers are used in many places, I want to reuse them so that i don’t duplicate lot of YAML code.

Following is my attempt at trying this without any luck.

responses:
        '200':
          description: Successful Request
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'
          headers:
                 - $ref: '#/components/headers/X-Total-Items'
                 - $ref: '#/components/headers/X-Total-Pages'
components
headers:
    X-Total-Items
      schema:
        type: integer
        description: test
  X-Total-Pages
      schema:
        type: integer
        description: test

Is this the right way?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
MikeRalphsoncommented, Dec 17, 2017

Hi @john1452 the thing to spot is that the headers field of the Response Object is a Map (of Header objects or References), not an array.

Your example should look something like:

openapi: 3.0.1
info:
  title: API
  version: 1.0.0
paths:
  /:
    get:
      responses:
        '200':
          description: Successful Request
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'
          headers:
            'X-Total-Items':
               $ref: '#/components/headers/xTotalItems'
            'X-Total-Pages':
               $ref: '#/components/headers/xTotalPages'
components:
  schemas:
    User: {}
  headers:
    xTotalItems:
      schema:
        type: integer
        description: test
    xTotalPages:
      schema:
        type: integer
        description: test

I have modified the names of the reusable header components to indicate clearly which is used as the actual header name, though they can of course be the same.

4reactions
MikeRalphsoncommented, Dec 17, 2017

Sorry, didn’t spot that. The header object has a description field of its own. Try moving the descriptions out of the schema objects (i.e. one level up).

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP headers - MDN Web Docs - Mozilla
HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its ......
Read more >
HTTP Response reference | WebStorm Documentation
The response object holds the information about a received HTTP Response (response content, headers, status, and so on) and provides access ...
Read more >
Request headers and responses - App Engine - Google Cloud
Use this reference page for details about what HTTP headers are supported as well as the request and response limits in App Engine....
Read more >
Common Response Headers - Amazon Simple Storage Service
Common Response Headers ; Content-Length. The length in bytes of the body in the response. Type: String. Default: None ; Content-Type. The MIME...
Read more >
List of HTTP header fields - Wikipedia
HTTP header fields are a list of strings sent and received by both the client program and server on every HTTP request and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found