Browse Source

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
Dmitri Shuralyov 10 years ago
committed by Glenn Lewis
parent
commit
b5e5babef3
3 changed files with 7 additions and 2 deletions
  1. +2
    -0
      github/activity_notifications_test.go
  2. +4
    -1
      github/github.go
  3. +1
    -1
      github/github_test.go

+ 2
- 0
github/activity_notifications_test.go View File

@ -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)


+ 4
- 1
github/github.go View File

@ -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)


+ 1
- 1
github/github_test.go View File

@ -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)
}
}


Loading…
Cancel
Save