12

I am trying to create an OpenAPI 3.0 definition for an existing API. It has a POST operation and takes header values as the input. Request body is empty. However the backend API was coded very poorly and is expecting request header Content-Type: application/json even though the body is empty.

How do I achieve this in OpenAPI 3.0? Looks like Content-Type is not accepted as a valid header parameter in OAS 3.0.

Helen
  • 87,344
  • 17
  • 243
  • 314
Raj Sharma
  • 135
  • 1
  • 1
  • 8

3 Answers3

12

You can add the requestBody with the application/json media type but no schema.

openapi: 3.0.2
...
paths:
  /something:
    post:
      parameters:
        ...
      requestBody:
        content:
          application/json: {}
Helen
  • 87,344
  • 17
  • 243
  • 314
  • i think we have a better way : requestBody: content: application/json: schema: # Request body contents type: object – saber tabatabaee yazdi Jun 20 '20 at 06:35
  • @sabertabatabaeeyazdi No. `type: object` means the request body is an object, and even an empty JSON object `{}` is not the same as empty request body. – Helen Jun 20 '20 at 08:13
3

with Insomnia when use:

  requestBody:
    content:
      application/json: {}

result is this: (preview)

insomnia requestbody empty json sample

but if use this:

  requestBody:
    content:
      application/json:
        schema:      # Request body contents
          type: object

result is : (preview)

openapi3 requestbody empty json object sample swagger

saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
1

Based on Open API 3 spec, your requestBody should be like the following:

   requestBody:
     required: true
       description: blabla.
         content:
            application/json:
                schema:
                   type: object
                   nullable: true
Polem
  • 131
  • 7