diff --git a/client.go b/client.go index 56e4469c..82454fe2 100644 --- a/client.go +++ b/client.go @@ -58,7 +58,7 @@ var ( hdrAuthorizationKey = http.CanonicalHeaderKey("Authorization") plainTextType = "text/plain; charset=utf-8" - jsonContentType = "application/json; charset=utf-8" + jsonContentType = "application/json" formContentType = "application/x-www-form-urlencoded" jsonCheck = regexp.MustCompile(`(?i:(application|text)/(json|.*\+json|json\-.*)(;|$))`) diff --git a/client_test.go b/client_test.go index 34b174f4..b3e63cda 100644 --- a/client_test.go +++ b/client_test.go @@ -241,12 +241,12 @@ func TestClientOptions(t *testing.T) { client.SetHostURL("http://httpbin.org") assertEqual(t, "http://httpbin.org", client.HostURL) - client.SetHeader(hdrContentTypeKey, jsonContentType) + client.SetHeader(hdrContentTypeKey, "application/json; charset=utf-8") client.SetHeaders(map[string]string{ hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.1", "X-Request-Id": strconv.FormatInt(time.Now().UnixNano(), 10), }) - assertEqual(t, jsonContentType, client.Header.Get(hdrContentTypeKey)) + assertEqual(t, "application/json; charset=utf-8", client.Header.Get(hdrContentTypeKey)) client.SetCookie(&http.Cookie{ Name: "default-cookie", diff --git a/request_test.go b/request_test.go index b2eaa8af..47e2b372 100644 --- a/request_test.go +++ b/request_test.go @@ -120,8 +120,8 @@ func TestPostJSONStringSuccess(t *testing.T) { defer ts.Close() c := dc() - c.SetHeader(hdrContentTypeKey, jsonContentType). - SetHeaders(map[string]string{hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.1", hdrAcceptKey: jsonContentType}) + c.SetHeader(hdrContentTypeKey, "application/json; charset=utf-8"). + SetHeaders(map[string]string{hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.1", hdrAcceptKey: "application/json; charset=utf-8"}) resp, err := c.R(). SetBody(`{"username":"testuser", "password":"testpass"}`). @@ -148,8 +148,8 @@ func TestPostJSONBytesSuccess(t *testing.T) { defer ts.Close() c := dc() - c.SetHeader(hdrContentTypeKey, jsonContentType). - SetHeaders(map[string]string{hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.7", hdrAcceptKey: jsonContentType}) + c.SetHeader(hdrContentTypeKey, "application/json; charset=utf-8"). + SetHeaders(map[string]string{hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.7", hdrAcceptKey: "application/json; charset=utf-8"}) resp, err := c.R(). SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)). @@ -166,7 +166,7 @@ func TestPostJSONBytesIoReader(t *testing.T) { defer ts.Close() c := dc() - c.SetHeader(hdrContentTypeKey, jsonContentType) + c.SetHeader(hdrContentTypeKey, "application/json; charset=utf-8") bodyBytes := []byte(`{"username":"testuser", "password":"testpass"}`) @@ -188,7 +188,7 @@ func TestPostJSONStructSuccess(t *testing.T) { c := dc().SetJSONEscapeHTML(false) resp, err := c.R(). - SetHeader(hdrContentTypeKey, jsonContentType). + SetHeader(hdrContentTypeKey, "application/json; charset=utf-8"). SetBody(user). SetResult(&AuthSuccess{}). Post(ts.URL + "/login") @@ -231,7 +231,7 @@ func TestPostJSONStructInvalidLogin(t *testing.T) { c.SetDebug(false) resp, err := c.R(). - SetHeader(hdrContentTypeKey, jsonContentType). + SetHeader(hdrContentTypeKey, "application/json; charset=utf-8"). SetBody(User{Username: "testuser", Password: "testpass1"}). SetError(AuthError{}). SetJSONEscapeHTML(false). @@ -254,7 +254,7 @@ func TestPostJSONErrorRFC7807(t *testing.T) { c := dc() resp, err := c.R(). - SetHeader(hdrContentTypeKey, jsonContentType). + SetHeader(hdrContentTypeKey, "application/json; charset=utf-8"). SetBody(User{Username: "testuser", Password: "testpass1"}). SetError(AuthError{}). Post(ts.URL + "/login?ct=problem") @@ -828,7 +828,7 @@ func TestPutJSONString(t *testing.T) { client.outputLogTo(ioutil.Discard) resp, err := client.R(). - SetHeaders(map[string]string{hdrContentTypeKey: jsonContentType, hdrAcceptKey: jsonContentType}). + SetHeaders(map[string]string{hdrContentTypeKey: "application/json; charset=utf-8", hdrAcceptKey: "application/json; charset=utf-8"}). SetBody(`{"content":"json content sending to server"}`). Put(ts.URL + "/json") diff --git a/resty_test.go b/resty_test.go index ab534ca0..31865315 100644 --- a/resty_test.go +++ b/resty_test.go @@ -131,7 +131,7 @@ func handleLoginEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) { } else if r.URL.Query().Get("ct") == "rpc" { w.Header().Set(hdrContentTypeKey, "application/json-rpc") } else { - w.Header().Set(hdrContentTypeKey, jsonContentType) + w.Header().Set(hdrContentTypeKey, "application/json") } if err != nil { @@ -192,7 +192,7 @@ func handleUsersEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) { var users []ExampleUser jd := json.NewDecoder(r.Body) err := jd.Decode(&users) - w.Header().Set(hdrContentTypeKey, jsonContentType) + w.Header().Set(hdrContentTypeKey, "application/json") if err != nil { t.Logf("Error: %v", err) w.WriteHeader(http.StatusBadRequest) @@ -240,7 +240,7 @@ func createPostServer(t *testing.T) *httptest.Server { t.Errorf("Error: could not read post body: %s", err.Error()) } t.Logf("Got query param: status=500 so we're returning the post body as response and a 500 status code. body: %s", string(body)) - w.Header().Set(hdrContentTypeKey, jsonContentType) + w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8") w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write(body) return @@ -249,7 +249,7 @@ func createPostServer(t *testing.T) *httptest.Server { var users []map[string]interface{} jd := json.NewDecoder(r.Body) err := jd.Decode(&users) - w.Header().Set(hdrContentTypeKey, jsonContentType) + w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8") if err != nil { t.Logf("Error: %v", err) w.WriteHeader(http.StatusBadRequest) @@ -391,7 +391,7 @@ func createAuthServer(t *testing.T) *httptest.Server { auth := r.Header.Get("Authorization") t.Logf("Bearer Auth: %v", auth) - w.Header().Set(hdrContentTypeKey, jsonContentType) + w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8") if !strings.HasPrefix(auth, "Bearer ") { w.Header().Set("Www-Authenticate", "Protected Realm") @@ -414,7 +414,7 @@ func createAuthServer(t *testing.T) *httptest.Server { auth := r.Header.Get("Authorization") t.Logf("Basic Auth: %v", auth) - w.Header().Set(hdrContentTypeKey, jsonContentType) + w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8") password, err := base64.StdEncoding.DecodeString(auth[6:]) if err != nil || string(password) != "myuser:basicauth" { @@ -472,7 +472,7 @@ func createGenServer(t *testing.T) *httptest.Server { if r.URL.Path == "/plaintext" { _, _ = w.Write([]byte("TestPut: plain text response")) } else if r.URL.Path == "/json" { - w.Header().Set(hdrContentTypeKey, jsonContentType) + w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8") _, _ = w.Write([]byte(`{"response":"json response"}`)) } else if r.URL.Path == "/xml" { w.Header().Set(hdrContentTypeKey, "application/xml")