Skip to content

Read APIError response body #121

@mxinden

Description

@mxinden

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:

return readResponse.ReadResponse(response{res}, cons)

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():

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions