Skip to content

Commit 35b7abd

Browse files
committed
Set content type header when payload is provided
1 parent 9a0798f commit 35b7abd

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

http/headers/headers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package headers
2+
3+
// HTTP headers
4+
const (
5+
ContentType = "Content-Type"
6+
Accept = "Accept"
7+
)

http/mediatypes/mediatypes.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package mediatypes
2+
3+
// Media types
4+
const (
5+
ApplicationJSON = "application/json"
6+
ApplicationJSONUtf8 = "application/json; charset=utf-8"
7+
)

remote.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919

2020
"github.com/blang/semver"
2121
"github.com/tebeka/selenium/firefox"
22+
"github.com/tebeka/selenium/http/headers"
23+
"github.com/tebeka/selenium/http/mediatypes"
2224
"github.com/tebeka/selenium/log"
2325
)
2426

@@ -59,15 +61,15 @@ type remoteWD struct {
5961
// server.
6062
var HTTPClient = http.DefaultClient
6163

62-
// jsonContentType is JSON content type.
63-
const jsonContentType = "application/json"
64-
6564
func newRequest(method string, url string, data []byte) (*http.Request, error) {
6665
request, err := http.NewRequest(method, url, bytes.NewBuffer(data))
6766
if err != nil {
6867
return nil, err
6968
}
70-
request.Header.Add("Accept", jsonContentType)
69+
if data != nil {
70+
request.Header.Add(headers.ContentType, mediatypes.ApplicationJSONUtf8)
71+
}
72+
request.Header.Add(headers.Accept, mediatypes.ApplicationJSON)
7173

7274
return request, nil
7375
}
@@ -148,19 +150,19 @@ func executeCommand(method, url string, data []byte) (json.RawMessage, error) {
148150
buf = prettyBuf.Bytes()
149151
}
150152
}
151-
debugLog("<- %s [%s]\n%s", response.Status, response.Header["Content-Type"], buf)
153+
debugLog("<- %s [%s]\n%s", response.Status, response.Header[headers.ContentType], buf)
152154
}
153155
if err != nil {
154156
return nil, errors.New(response.Status)
155157
}
156158

157-
fullCType := response.Header.Get("Content-Type")
159+
fullCType := response.Header.Get(headers.ContentType)
158160
cType, _, err := mime.ParseMediaType(fullCType)
159161
if err != nil {
160-
return nil, fmt.Errorf("got content type header %q, expected %q", fullCType, jsonContentType)
162+
return nil, fmt.Errorf("got content type header %q, expected %q", fullCType, mediatypes.ApplicationJSON)
161163
}
162-
if cType != jsonContentType {
163-
return nil, fmt.Errorf("got content type %q, expected %q", cType, jsonContentType)
164+
if cType != mediatypes.ApplicationJSON {
165+
return nil, fmt.Errorf("got content type %q, expected %q", cType, mediatypes.ApplicationJSON)
164166
}
165167

166168
reply := new(serverReply)

0 commit comments

Comments
 (0)