Skip to content

Commit

Permalink
handle raw data correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Mustafa Kuscu committed Oct 26, 2020
1 parent 4c6fb87 commit 67c9404
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 14 additions & 4 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 67c9404

Please # to comment.