FAQ
Answers to common questions collected from GitHub issues.
Client
How do I disable TLS certificate verification?
Use TLSClientOptions with InsecureSkipVerify:
Then pass the resulting *http.Client to your transport.
Why is request.ContentLength zero when I send a body?
A streaming body (e.g. from bytes.NewReader) is sent with chunked transfer encoding.
The runtime cannot know the content length of an arbitrary stream unless you explicitly
set it on the request. If you need ContentLength populated, set it yourself before
submitting.
How do I read the error response body from an APIError?
The client’s Submit() closes the response body after reading. To access error details,
define your error responses (including a default response) in the Swagger spec with a
schema. The generated client will then deserialize the error body into a typed struct
that you can access via type assertion:
Without a response schema in the spec, the body is discarded and only the status code
is available in the runtime.APIError.
How do I register custom MIME types (e.g. application/problem+json)?
The default client runtime ships with a fixed set of consumers/producers. Register custom ones on the transport:
The same approach works for any non-standard MIME type such as application/pdf
(use runtime.ByteStreamConsumer()), application/hal+json, or
application/vnd.error+json (use runtime.JSONConsumer()).
Middleware
How do I access the authenticated Principal from an OperationHandler?
Use the context helpers from the middleware package:
These extract values that the middleware pipeline stored in the request context during authentication and routing.
Can I run authentication on requests that don’t match a route?
No. Authentication is determined dynamically per route from the OpenAPI spec (each operation declares its own security requirements). The middleware pipeline authenticates after routing, so unmatched requests are never authenticated.
How do I share context values across middlewares when using an external router?
The go-openapi router creates a new request context during route resolution. Context values set after routing (e.g. during auth) are not visible to middlewares that run before the router in the chain.
The recommended pattern is to use a pointer-based shared struct:
Because the struct is shared by pointer, mutations are visible regardless of which request copy carries the context.
Can I use this library to validate requests/responses without code generation?
Yes. Use the routing and validation middleware from the middleware package with
an untyped API. Load your spec with loads.Spec(), then wire up
middleware.NewRouter() to get request validation against the spec without
needing go-swagger generated code. See the middleware/untyped package for
examples.
How do I configure Swagger UI to show multiple specs?
SwaggerUIOpts supports the urls parameter for listing multiple spec files in
the Swagger UI explore bar. Configure it instead of the single url parameter.
Documentation
Where can I find middleware documentation?
- GoDoc — API reference
- go-swagger middleware guide — usage patterns
- go-swagger FAQ — common questions