apitest
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()
MethodBehaviour
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:

BuilderContent-Type
JSON, JSONFromFileapplication/json
GraphQLQuery, GraphQLRequestapplication/json
FormDataapplication/x-www-form-urlencoded
MultipartFormData, MultipartFilemultipart/form-data with the boundary

BasicAuth sets the Authorization header and Cookie sets the Cookie header.

On this page