createRequest()function
createRequest(method: RequestMethod, url: PossibleURL, payload: unknown, options: RequestOptions = {}, caller: AnyCaller = createRequest): Request| Param | Type | |
|---|---|---|
method | RequestMethod | The HTTP method. required |
url | PossibleURL | The target URL. required |
payload | unknown | The body payload, whose type selects the content type. required |
options | RequestOptions | Additional request options. Defaults to {} |
caller | AnyCaller | 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 |
Create a Request instance with a valid content type based on the body.
undefinedornullare sent with no body.FormDatais sent withmultipart/formdatastringis sent withtext/plainheader.- 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,
GETandHEADrequests cannot contain a body
Examples
createRequest("POST", "https://api.example.com/items", { name: "abc" }) // JSON Request