loic_midy
loic_midy

Reputation: 103

openapi 3 : how to specify that header parameters are JSON

I have a web service with JWT authentification. Here is an example of using the web service

headers = {'content-type': 'application/json'}
    authentification={"username": "test", "password":"test"}
    r = requests.post("http://localhost:8888/login",headers=headers,json=authentification)
    dico= r.json() 
    TOKEN=dico["access_token"]
    print("TOKEN",TOKEN)

I try to document this in YAML in openapi 3. Here is my code

paths: 
  /login: 
    post: 
      summary: authentification
      parameters:
        - in: header
          name: username
          schema:
            type: string
          required: true
          description: username
        - in: header
          name: password
          schema:
            type: string
          required: true
          description: mot de passe
      responses:
        '200':
          description: OK

What is missing is that I don't know how to specify that the header parameters must be in json. How can I do it?

Yours sincerely Loïc

Upvotes: 1

Views: 1168

Answers (1)

Helen
Helen

Reputation: 97560

In your example, username and password are not header parameters, they are sent in the JSON request body:

{"username": "...", "password": "..."}

In OpenAPI 3.0, request body is defined using the requestBody keyword:

openapi: 3.0.0
...

paths:
  /login: 
    post: 
      summary: authentification
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LoginRequest'
      responses:
        '200':
          description: OK

components:
  schemas:
    LoginRequest:
      type: object
      required:
        - username
        - password
      properties:
        username:
          type: string
        password:
          type: string

More info: Describing Request Body

Upvotes: 1

Related Questions