Home > other >  Designing an API OpenAPI 3.0 - header parameter Content type are ignored
Designing an API OpenAPI 3.0 - header parameter Content type are ignored

Time:04-20

This question is really a followup to an earlier question here

what I am trying to do is force the consumer of my api to ONLY send Content-Type as application/json

My environment is : Mule 4.4 runtime ( on prem , no usage of Anypoint Platform )
I am defining the spec in OAS 3.0 and am using swagger editor to create and edit specification

I have the following defined :

/user:
post:
  parameters:
    - in: header
      name: Content-Type
      required: true
      schema:
        type: string
        enum:
          - application/json

and I can see a warning:

Header Parameters named 'Content-type' are ignored. The values for the 'Content-Type' header are defined by 'RequestBody.content.media-type

Tried googling and found one link here But it simply states :

Make sure that you do not use the restricted values as header parameter names.

which really does not explain of how to enforce this criteria ?

As a side note - if I copy paste the same api spec in Design centre ( Anypoint platform ) it does not complain and infact mule runtime correctly validates and rejects requests that do not have application/json in content-type

Thanks

CodePudding user response:

In OpenAPI (or even RAML) we don't treat the Content-Type header as a generic HTTP header. This applies to both requests and responses. As part of the specification you can define the media types for each request. The implementation will take care of processing it correctly and validate the right Content-Type header is used at execution time. Since you are trying to set it manually the implementation is giving you an expected warning.

Example:

paths:
  /services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX:
    post:
      requestBody:
        content:        
          application/json:
  • Related