Requests
Headers
Set request headers one at a time, from a map, or with the Content-Type shortcut.
apitest.Handler(handler).
Get("/hello").
Header("Authorization", "Bearer token").
Headers(map[string]string{
"X-Request-Id": "12345",
"X-Tenant": "acme",
}).
ContentType("application/json").
Expect(t).
Status(http.StatusOK).
End()| Method | Behaviour |
|---|---|
Header(key, value) | Adds a value for the header. |
Headers(map[string]string) | Adds a value for each entry in the map. |
ContentType(value) | Sets Content-Type, replacing any earlier value. |
The methods can be combined and called as often as needed.
Repeated headers
Header and Headers add values rather than replace them, so calling Header twice with the same key sends both values:
apitest.Handler(handler).
Get("/hello").
Header("Accept", "application/json").
Header("Accept", "text/plain").
Expect(t).
Status(http.StatusOK).
End()Header names
Header names are stored in their canonical form, so x-request-id and X-Request-Id refer to the same header. This matches how Go's net/http treats headers.
Headers set for you
Some request builders set Content-Type themselves:
| Builder | Content-Type |
|---|---|
JSON, JSONFromFile | application/json |
GraphQLQuery, GraphQLRequest | application/json |
FormData | application/x-www-form-urlencoded |
MultipartFormData, MultipartFile | multipart/form-data with the boundary |
BasicAuth sets the Authorization header and Cookie sets the Cookie header.