diff --git a/http.go b/http.go index 96f79b7..59e505d 100644 --- a/http.go +++ b/http.go @@ -127,13 +127,23 @@ func (h *HttpClient) do(rawurl, method string, in, out interface{}) error { return httpError } - jsonOut := []byte("") if out != nil { - jsonOut, _ = json.MarshalIndent(out, "", " ") + reader := resp.Body + rawData, err := ioutil.ReadAll(reader) + if err != nil { + fmt.Printf("Response body reader: %+v", err) + return err + } + rawDataStr := string(rawData) if h.debug { - fmt.Printf("Response status %s from %s at %s, data: %s\n", resp.Status, resp.Request.URL, time.Now().Format(time.RFC3339), jsonOut) + fmt.Printf("Response status %s from %s at %s, data: %s\n", resp.Status, resp.Request.URL, time.Now().Format(time.RFC3339), rawDataStr) + } + err = json.NewDecoder(bytes.NewReader(rawData)).Decode(out) + if err != nil { + if h.debug { + fmt.Printf("Response json decoding error data: %s err: %+v\n", rawDataStr, err) + } } - return json.NewDecoder(resp.Body).Decode(out) } else { if h.debug { fmt.Printf("Response status %s from %s at %s\n", resp.Status, resp.Request.URL, time.Now().Format(time.RFC3339)) diff --git a/http_test.go b/http_test.go index cc80874..99f961d 100644 --- a/http_test.go +++ b/http_test.go @@ -17,6 +17,15 @@ type PostIn struct { DummyField string } +func Test_http_echo(t *testing.T) { + client := NewHttpClient("", WithDebug()) + in := &PostIn{ + DummyField: "a123456", + } + out := &PostIn{} + err := client.Post("http://127.0.0.1:3000/notif", in, out) + require.NoError(t, err) +} func Test_debug_http_postman(t *testing.T) { client := NewHttpClient("https://postman-echo.com/post", WithDebug()) in := &PostIn{