-
Notifications
You must be signed in to change notification settings - Fork 149
Description
I make a request via a client generated based on a swagger spec generated via the goswagger.io project with an invalid json property. The back-end returns a HTTP 422 with details in the response body. Reading the response body is crucial to debug the problem.
go-openapi/runtime/client/runtime.go.Submit() calls readResponse.ReadResponse which returns an APIError with the response body embedded:
Line 422 in 231d787
| return readResponse.ReadResponse(response{res}, cons) |
Lines 55 to 59 in 231d787
| type APIError struct { | |
| OperationName string | |
| Response interface{} | |
| Code int | |
| } |
Problem is, that this embedded response body is closed via a defer in go-openapi/runtime/client/runtime.go.Submit():
Line 395 in 231d787
| defer res.Body.Close() |
Thereby casting the error and reading the response body later on fails with http: read on closed response body:
apiError, ok := err.(*runtime.APIError)
if !ok {
// handle
}
response, ok := apiError.Response.(runtime.ClientResponse)
if !ok {
// handle
}
body, err := ioutil.ReadAll(response.Body())
if err != nil {
// fails with "http: read on closed response body"
}
fmt.Println(string(body))How can I access the APIError response body? Might it be possible to embed the body as a string inside the APIError? Let me know if you need more info.