Request
Make HTTP requests
Request is used to make HTTP/HTTPS network requests, supporting various response formats.
Use Request to:
Note
Use allowInsecureRequest with caution
Examples
let req = new Request("https://api.example.com/data");
let json = await req.loadJSON();
console.log(json);let req = new Request("https://api.example.com/post");
req.method = "POST";
req.headers = { "Content-Type": "application/json" };
req.body = JSON.stringify({ name: "test" });
let response = await req.loadJSON();Properties
url
- Type:
String
Request URL
The URL to send the request to.
req.url = "https://example.com"method
- Type:
String
HTTP method
The HTTP method to use. Defaults to "GET".
req.method = "POST"headers
- Type:
[String: String]
Request headers
HTTP headers to include in the request.
req.headers = { "Authorization": "Bearer token" }body
- Type:
String
Request body
The body content for POST/PUT requests.
req.body = JSON.stringify({ key: "value" })timeoutInterval
- Type:
Double
Timeout interval
Request timeout in seconds. Defaults to 60.
req.timeoutInterval = 30allowInsecureRequest
- Type:
Bool
Allow insecure requests
Set to true to allow requests to servers with invalid SSL certificates. Use with caution, only for development purposes.
req.allowInsecureRequest = trueresponse
- Type:
JSValue
Response object
Contains response information after the request completes. Includes statusCode, headers, cookies, and mimeType.
let status = req.response.statusCodeMethods
load
Load raw data
Performs the request and returns raw Data object.
load(callback): voidcallback- Callback with result
Returns: Promise that resolves to Data
Example:
let data = await req.load()loadString
Load as string
Performs the request and returns response as string.
loadString(callback): voidcallback- Callback with result
Returns: Promise that resolves to string
Example:
let text = await req.loadString()loadJSON
Load as JSON
Performs the request and parses response as JSON.
loadJSON(callback): voidcallback- Callback with result
Returns: Promise that resolves to parsed JSON object
Example:
let json = await req.loadJSON()loadImage
Load as image
Performs the request and returns response as image data.
loadImage(callback): voidcallback- Callback with result
Returns: Promise that resolves to image info with base64, width, height
Example:
let img = await req.loadImage()addParameterToMultipart
Add parameter to multipart form
Adds a text parameter to a multipart form request.
addParameterToMultipart(name, value): voidname- Parameter namevalue- Parameter value
Example:
req.addParameterToMultipart("field", "value")addFileDataToMultipart
Add file to multipart form
Adds a file to a multipart form request.
addFileDataToMultipart(data, mimeType, name, filename): voiddata- File data as base64 stringmimeType- MIME type of the filename- Form field namefilename- File name
Example:
req.addFileDataToMultipart(base64Data, "image/png", "file", "image.png")