From b5e5babef39c18002f177a134fc49dc5013374ba Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Mon, 18 Jul 2016 20:22:51 -0400 Subject: [PATCH] Set Content-Type to "application/json" for request bodies According to https://developer.github.com/v3/#parameters: For POST, PATCH, PUT, and DELETE requests, parameters not included in the URL should be encoded as JSON with a Content-Type of 'application/json' Closes #407. Change-Id: Iee60a3ed6e06b85cc64f687fd28e7663c1fc34e3 --- github/activity_notifications_test.go | 2 ++ github/github.go | 5 ++++- github/github_test.go | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/github/activity_notifications_test.go b/github/activity_notifications_test.go index 500c57d..8f6a581 100644 --- a/github/activity_notifications_test.go +++ b/github/activity_notifications_test.go @@ -73,6 +73,7 @@ func TestActivityService_MarkNotificationsRead(t *testing.T) { mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") + testHeader(t, r, "Content-Type", "application/json") testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n") w.WriteHeader(http.StatusResetContent) @@ -90,6 +91,7 @@ func TestActivityService_MarkRepositoryNotificationsRead(t *testing.T) { mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") + testHeader(t, r, "Content-Type", "application/json") testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n") w.WriteHeader(http.StatusResetContent) diff --git a/github/github.go b/github/github.go index e2522d2..591bc54 100644 --- a/github/github.go +++ b/github/github.go @@ -223,6 +223,9 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ return nil, err } + if body != nil { + req.Header.Set("Content-Type", "application/json") + } req.Header.Set("Accept", mediaTypeV3) if c.UserAgent != "" { req.Header.Set("User-Agent", c.UserAgent) @@ -246,7 +249,7 @@ func (c *Client) NewUploadRequest(urlStr string, reader io.Reader, size int64, m } req.ContentLength = size - if len(mediaType) == 0 { + if mediaType == "" { mediaType = defaultMediaType } req.Header.Set("Content-Type", mediaType) diff --git a/github/github_test.go b/github/github_test.go index 39b0766..e166bb0 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -101,7 +101,7 @@ func testFormValues(t *testing.T, r *http.Request, values values) { func testHeader(t *testing.T, r *http.Request, header string, want string) { if got := r.Header.Get(header); got != want { - t.Errorf("Header.Get(%q) returned %s, want %s", header, got, want) + t.Errorf("Header.Get(%q) returned %q, want %q", header, got, want) } }