createRequest()function

createRequest(method: RequestMethod, url: PossibleURL, payload: unknown, options: RequestOptions = {}, caller: AnyCaller = createRequest): Request
ParamType
methodRequestMethod
The HTTP method. required
urlPossibleURL
The target URL. required
payloadunknown
The body payload, whose type selects the content type. required
optionsRequestOptions
Additional request options. Defaults to {}
callerAnyCaller
Function to attribute a thrown error to (defaults to createRequest). Defaults to createRequest
Return
Request
A Request with a content type chosen to match payload.
Throws
RequiredError
if this is a HEAD or GET request but body is not a data object.

Create a Request instance with a valid content type based on the body.

  • undefined or null are sent with no body.
  • FormData is sent with multipart/formdata
  • string is sent with text/plain header.
  • Anything else is sent as application/json
  • Expects a fully valid URL (any {placeholders} in the URL are not considered).
  • As per the HTTP spec, GET and HEAD requests cannot contain a body

Examples

createRequest("POST", "https://api.example.com/items", { name: "abc" }) // JSON Request