Skip to content

Commit c95a971

Browse files
committed
Set content type header when payload is provided
1 parent edf31bb commit c95a971

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

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
@@ -17,6 +17,8 @@ import (
1717

1818
"github.com/blang/semver"
1919
"github.com/tebeka/selenium/firefox"
20+
"github.com/tebeka/selenium/http/headers"
21+
"github.com/tebeka/selenium/http/mediatypes"
2022
"github.com/tebeka/selenium/log"
2123
)
2224

@@ -57,15 +59,15 @@ type remoteWD struct {
5759
// server.
5860
var HTTPClient = http.DefaultClient
5961

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

7072
return request, nil
7173
}
@@ -142,19 +144,19 @@ func (wd *remoteWD) execute(method, url string, data []byte) (json.RawMessage, e
142144
buf = prettyBuf.Bytes()
143145
}
144146
}
145-
debugLog("<- %s [%s]\n%s", response.Status, response.Header["Content-Type"], buf)
147+
debugLog("<- %s [%s]\n%s", response.Status, response.Header[headers.ContentType], buf)
146148
}
147149
if err != nil {
148150
return nil, errors.New(response.Status)
149151
}
150152

151-
fullCType := response.Header.Get("Content-Type")
153+
fullCType := response.Header.Get(headers.ContentType)
152154
cType, _, err := mime.ParseMediaType(fullCType)
153155
if err != nil {
154-
return nil, fmt.Errorf("got content type header %q, expected %q", fullCType, jsonContentType)
156+
return nil, fmt.Errorf("got content type header %q, expected %q", fullCType, mediatypes.ApplicationJSON)
155157
}
156-
if cType != jsonContentType {
157-
return nil, fmt.Errorf("got content type %q, expected %q", cType, jsonContentType)
158+
if cType != mediatypes.ApplicationJSON {
159+
return nil, fmt.Errorf("got content type %q, expected %q", cType, mediatypes.ApplicationJSON)
158160
}
159161

160162
reply := new(serverReply)

0 commit comments

Comments
 (0)