Reputation: 2189
Is it okay according to the HTTP spec to have a request that (for example) does the following:
GET https://www.example.com/api/images/1 -> 200 OK, Content Type "image/png"
GET https://www.example.com/api/images/2 -> 404 NOT FOUND, Content Type "application/json"
We're having trouble with this in Swagger since you have to specify the Content-Type of the response before making the request, so I figured I'd ask whether what we want to do is even acceptable according to the HTTP spec before we continue digging into how it can be solved with Swagger
Upvotes: 2
Views: 93
Reputation: 97847
Yes, it's perfectly fine for an URL to return different HTTP status codes depending on the result of the request. It's also OK to return different media types, e.g. based on the Accept
header (aka content negotiation) or other conditions.
In OpenAPI/Swagger 2.0, your example can be described like this:
paths:
/something:
get:
produces:
- image/png
- application/json
responses:
'200':
description: A PNG file
schema:
type: file
'404':
description: Not Found
schema:
type: object
# properties:
# ...
OpenAPI 3.0 improves response definitions and lets you specify the media types for specific status codes:
responses:
'200':
description: A PNG image
content:
image/png:
schema:
type: string
format: binary
'404':
description: Not found
content:
application/json:
schema:
type: object
# properties:
# ...
Upvotes: 2