From 2b5eb362ddb094ad69155cdfd630f4a6d699b2fe Mon Sep 17 00:00:00 2001 From: Yong Zhang Date: Mon, 16 Dec 2019 22:56:14 +0800 Subject: [PATCH 1/3] Fixes unexpected error when request using pulsar 2.4.2 proxy --- *Motivation* When sending a GET or DELETE request to the Pulsar proxy, the server always threw an IllegalArgumentException. By my test that's because we are setting the Content-Type for a request without the request body. We remove the Content-Type if the request is GET or DELETE. *Modifications* - Remove the header Content-Type for the GET and DELETE requests --- pkg/cli/client.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/cli/client.go b/pkg/cli/client.go index 0e201549c..bd48d44a5 100644 --- a/pkg/cli/client.go +++ b/pkg/cli/client.go @@ -63,11 +63,15 @@ func (c *Client) doRequest(r *request) (*http.Response, error) { return nil, err } - if r.contentType != "" { - req.Header.Set("Content-Type", r.contentType) - } else { - // add default headers - req.Header.Set("Content-Type", "application/json") + switch r.method { + case http.MethodPut: + fallthrough + case http.MethodPost: + if r.contentType != "" { + req.Header.Set("Content-Type", r.contentType) + } else { + req.Header.Set("Content-Type", "application/json") + } } req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", c.useragent()) From bac58657d97dff7c4ee1a9b65f9d27108295a685 Mon Sep 17 00:00:00 2001 From: Yong Zhang Date: Wed, 18 Dec 2019 10:56:25 +0800 Subject: [PATCH 2/3] Add content type when the request body not nil --- pkg/cli/client.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/pkg/cli/client.go b/pkg/cli/client.go index bd48d44a5..aa690d3aa 100644 --- a/pkg/cli/client.go +++ b/pkg/cli/client.go @@ -63,15 +63,11 @@ func (c *Client) doRequest(r *request) (*http.Response, error) { return nil, err } - switch r.method { - case http.MethodPut: - fallthrough - case http.MethodPost: - if r.contentType != "" { - req.Header.Set("Content-Type", r.contentType) - } else { - req.Header.Set("Content-Type", "application/json") - } + switch { + case req.Body != nil: + req.Header.Set("Content-Type", "application/json") + case r.contentType != "": + req.Header.Set("Content-Type", r.contentType) } req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", c.useragent()) From 37f5988c7c87d688da07fcbb42d937a073b3d12e Mon Sep 17 00:00:00 2001 From: Yong Zhang Date: Wed, 18 Dec 2019 12:25:34 +0800 Subject: [PATCH 3/3] Add specified content type at first --- pkg/cli/client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/cli/client.go b/pkg/cli/client.go index aa690d3aa..ccbe3f9d5 100644 --- a/pkg/cli/client.go +++ b/pkg/cli/client.go @@ -63,12 +63,12 @@ func (c *Client) doRequest(r *request) (*http.Response, error) { return nil, err } - switch { - case req.Body != nil: - req.Header.Set("Content-Type", "application/json") - case r.contentType != "": + if r.contentType != "" { req.Header.Set("Content-Type", r.contentType) + } else if req.Body != nil { + req.Header.Set("Content-Type", "application/json") } + req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", c.useragent())