Skip to content

Commit 82ad98d

Browse files
committed
add Request URI and response body to debugging output
1 parent b8d3db5 commit 82ad98d

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

api/client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package api
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io"
78
"net/http"
89
"strings"
910

1011
"github.com/exercism/cli/config"
12+
"github.com/exercism/cli/debug"
1113
)
1214

1315
const (
@@ -54,10 +56,13 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (*http.Request,
5456

5557
// Do performs an http.Request and optionally parses the response body into the given interface.
5658
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
59+
debug.Println("Request", req.Method, req.URL)
60+
5761
res, err := c.client.Do(req)
5862
if err != nil {
5963
return nil, err
6064
}
65+
debug.Printf("Response StatusCode=%d\n", res.StatusCode)
6166

6267
switch res.StatusCode {
6368
case http.StatusNoContent:
@@ -71,7 +76,13 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
7176
default:
7277
if v != nil {
7378
defer res.Body.Close()
74-
if err := json.NewDecoder(res.Body).Decode(v); err != nil {
79+
80+
var bodyCopy bytes.Buffer
81+
body := io.TeeReader(res.Body, &bodyCopy)
82+
83+
err := json.NewDecoder(body).Decode(v)
84+
debug.Printf("Response Body\n%s\n\n", bodyCopy.String())
85+
if err != nil {
7586
return nil, fmt.Errorf("error parsing API response - %s", err)
7687
}
7788
}

0 commit comments

Comments
 (0)