Skip to content

Commit cbadd4b

Browse files
committed
update docs related to server response
1 parent e6cd21c commit cbadd4b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

docs/src/userguide.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ Optional middlewares can be one or more of:
198198
The order in which middlewares are invoked is:
199199
`init |> read |> pre_validation |> validate |> pre_invoke |> invoke |> post_invoke`
200200

201+
## Responses
202+
203+
The server APIs can return the Julia type that is specified in the OpenAPI specification. The response is serialized as JSON and sent back to the client. The default HTTP response code used in this case is 200.
204+
205+
To return a custom HTTP response code, the server API can return a `HTTP.Response` instance directly. The OpenAPI package provides a overridden constructor for `HTTP.Response` that takes the desired HTTP code and the Julia struct that needs to be serialized as JSON and sent back to the client. It also sets the `Content-Type` header to `application/json`.
206+
207+
```julia
208+
HTTP.Response(code::Integer, o::APIModel)
209+
```
210+
211+
Structured error messages can also be returned in similar fashion. Any uncaught exception thrown by the server API is caught and converted into a `HTTP.Response` instance with the HTTP code set to 500 and the exception message as the response body.
212+
201213
## Streaming Responses
202214

203215
Some OpenAPI implementations implement streaming of responses by sending more than one items in the response, each of which is of the type declared as the return type in the specification. E.g. the [Twitter OpenAPI specification](https://api.twitter.com/2/openapi.json) that keeps sending tweets in JSON like this forever:

0 commit comments

Comments
 (0)