ReadJSON binds "dest" to the response's body. After this call, the response body reader is closed.
(ctx context.Context, dest interface{}, method, urlpath string, payload interface{}, opts ...RequestOption)
| 398 | // ReadJSON binds "dest" to the response's body. |
| 399 | // After this call, the response body reader is closed. |
| 400 | func (c *Client) ReadJSON(ctx context.Context, dest interface{}, method, urlpath string, payload interface{}, opts ...RequestOption) error { |
| 401 | if payload != nil { |
| 402 | opts = append(opts, RequestHeader(true, contentTypeKey, contentTypeJSON)) |
| 403 | } |
| 404 | |
| 405 | resp, err := c.Do(ctx, method, urlpath, payload, opts...) |
| 406 | if err != nil { |
| 407 | return err |
| 408 | } |
| 409 | defer c.DrainResponseBody(resp) |
| 410 | |
| 411 | if resp.StatusCode >= http.StatusBadRequest { |
| 412 | return ExtractError(resp) |
| 413 | } |
| 414 | |
| 415 | // DBUG |
| 416 | // b, _ := io.ReadAll(resp.Body) |
| 417 | // println(string(b)) |
| 418 | // return json.Unmarshal(b, &dest) |
| 419 | |
| 420 | if dest != nil { |
| 421 | return json.NewDecoder(resp.Body).Decode(&dest) |
| 422 | } |
| 423 | |
| 424 | return json.NewDecoder(resp.Body).Decode(&dest) |
| 425 | } |
| 426 | |
| 427 | // ReadPlain like ReadJSON but it accepts a pointer to a string or byte slice or integer |
| 428 | // and it reads the body as plain text. |