diff --git a/examples/cmd/context/main.go b/examples/cmd/context/main.go index 62b6f72..fb82605 100644 --- a/examples/cmd/context/main.go +++ b/examples/cmd/context/main.go @@ -55,6 +55,8 @@ func (r *TodoResponse) SetStatusCode(code int) error { return nil } +func (r *TodoResponse) SetHeaders(headers map[string]string) error { return nil } + func main() { restClient := restclientgo.New("https://jsonplaceholder.typicode.com") diff --git a/examples/cmd/delete/main.go b/examples/cmd/delete/main.go index 856c46a..400c86b 100644 --- a/examples/cmd/delete/main.go +++ b/examples/cmd/delete/main.go @@ -49,6 +49,8 @@ func (r *DeletePostResponse) SetStatusCode(code int) error { return nil } +func (r *DeletePostResponse) SetHeaders(headers map[string]string) error { return nil } + func main() { restClient := restclientgo.New("https://jsonplaceholder.typicode.com") diff --git a/examples/cmd/get/main.go b/examples/cmd/get/main.go index 8624cc3..1aa6b87 100644 --- a/examples/cmd/get/main.go +++ b/examples/cmd/get/main.go @@ -53,6 +53,8 @@ func (r *TodoResponse) SetStatusCode(code int) error { return nil } +func (r *TodoResponse) SetHeaders(headers map[string]string) error { return nil } + func main() { restClient := restclientgo.New("https://jsonplaceholder.typicode.com") diff --git a/examples/cmd/patch/main.go b/examples/cmd/patch/main.go index e7bfee3..8b368ac 100644 --- a/examples/cmd/patch/main.go +++ b/examples/cmd/patch/main.go @@ -62,6 +62,8 @@ func (r *UpdatePostResponse) SetStatusCode(code int) error { return nil } +func (r *UpdatePostResponse) SetHeaders(headers map[string]string) error { return nil } + func main() { restClient := restclientgo.New("https://jsonplaceholder.typicode.com") diff --git a/examples/cmd/post/main.go b/examples/cmd/post/main.go index ccee31b..d943eed 100644 --- a/examples/cmd/post/main.go +++ b/examples/cmd/post/main.go @@ -61,6 +61,8 @@ func (r *CreatePostResponse) SetStatusCode(code int) error { return nil } +func (r *CreatePostResponse) SetHeaders(headers map[string]string) error { return nil } + func main() { restClient := restclientgo.New("https://jsonplaceholder.typicode.com") diff --git a/examples/cmd/put/main.go b/examples/cmd/put/main.go index cd98143..2b2c3ab 100644 --- a/examples/cmd/put/main.go +++ b/examples/cmd/put/main.go @@ -62,6 +62,8 @@ func (r *UpdatePostResponse) SetStatusCode(code int) error { return nil } +func (r *UpdatePostResponse) SetHeaders(headers map[string]string) error { return nil } + func main() { restClient := restclientgo.New("https://jsonplaceholder.typicode.com") diff --git a/examples/cmd/url_values/main.go b/examples/cmd/url_values/main.go index 6376226..e3bde1d 100644 --- a/examples/cmd/url_values/main.go +++ b/examples/cmd/url_values/main.go @@ -60,6 +60,8 @@ func (r *CommentsResponse) SetStatusCode(code int) error { return nil } +func (r *CommentsResponse) SetHeaders(headers map[string]string) error { return nil } + func main() { restClient := restclientgo.New("https://jsonplaceholder.typicode.com") diff --git a/restclientgo.go b/restclientgo.go index c4ac816..593ce6d 100644 --- a/restclientgo.go +++ b/restclientgo.go @@ -56,6 +56,8 @@ type Response interface { AcceptContentType() string // SetStatusCode sets the HTTP response status code. SetStatusCode(code int) error + // SetHeaders sets the HTTP response headers. + SetHeaders(headers map[string]string) error } // New creates a new RestClient. @@ -148,6 +150,16 @@ func (r *RestClient) do(ctx context.Context, method httpMethod, request Request, return nil } + headers := make(map[string]string) + for k, v := range httpResponse.Header { + headers[k] = strings.Join(v, ",") + } + + err = response.SetHeaders(headers) + if err != nil { + return err + } + if response.AcceptContentType() == "" { response.SetBody(httpResponse.Body) return nil diff --git a/restclientgo_test.go b/restclientgo_test.go index df597cc..8b78f64 100644 --- a/restclientgo_test.go +++ b/restclientgo_test.go @@ -36,6 +36,7 @@ func (r *TodoResponse) SetStatusCode(code int) error { r.HTTPStatusCode = code return nil } +func (r *TodoResponse) SetHeaders(headers map[string]string) error { return nil } //--------------------------------------------- @@ -64,6 +65,7 @@ func (r *DeletePostResponse) SetStatusCode(code int) error { r.HTTPStatusCode = code return nil } +func (r *DeletePostResponse) SetHeaders(headers map[string]string) error { return nil } // --------------------------------------------- @@ -104,6 +106,7 @@ func (r *UpdatePostResponse) SetStatusCode(code int) error { r.HTTPStatusCode = code return nil } +func (r *UpdatePostResponse) SetHeaders(headers map[string]string) error { return nil } // --------------------------------------------- @@ -145,6 +148,7 @@ func (r *CreatePostResponse) SetStatusCode(code int) error { r.HTTPStatusCode = code return nil } +func (r *CreatePostResponse) SetHeaders(headers map[string]string) error { return nil } // ---------------------------------------------