The Network library provides a powerful interface for making asynchronous HTTP requests using the async/await pattern. It supports all standard HTTP methods and global header management.
The Network library is built-in and globally available as Network.
CursorScript's await operator automatically handles both the success and failure cases of an asynchronous operation by returning a tuple: (data, error).
const (data, error) = await Network.get("https://api.example.com/data");
if (error) {
printError("Request failed:", error);
} else {
print("Received data:", data);
}
Adds a single global header that will be sent with all subsequent requests.
- key: String - The header name (e.g., "Authorization").
- value: String - The header value.
Sets multiple global headers at once.
- headersObj: Object - A map of key-value pairs.
Removes all previously set global headers.
All request methods are asynchronous and should be used with await.
Performs an HTTP GET request.
- url: String - The destination URL.
- options: Object (Optional) - Request options such as custom headers.
- Returns: Promise resolving to the parsed response body.
Performs an HTTP POST request.
- url: String - The destination URL.
- body: Any - Data to be sent in the request body (will be JSON stringified).
- options: Object (Optional) - Request options.
Performs an HTTP PUT request.
Performs an HTTP PATCH request.
Performs an HTTP DELETE request.
Performs an HTTP HEAD request.
- Returns: Promise resolving to an object containing the response headers.
- Automatic JSON Parsing: If the response header
Content-Typecontainsapplication/json, thedatawill be automatically parsed into a CursorScript Object or Array. Otherwise, it returns a String. - Error Handling: On network failure or non-OK HTTP status codes (4xx, 5xx), the
errorpart of theawaittuple will contain a descriptive error string, anddatawill benull.
Network.AddHeader("Authorization", "Bearer my-token-123");
const payload = {
title: "Awesome Post",
content: "This was sent using CursorScript!"
};
const (response, error) = await Network.post("https://api.example.com/posts", payload);
if (error) {
printError("Post failed:", error);
} else {
print("Post created successfully:", response.id);
}
const (data, err) = await Network.get("https://api.myData.org", {
headers: {
"X-Custom-ID": "9988"
}
});
const (headers, err) = await Network.head("https://example.com/file.zip");
if (!err) {
print("File size:", headers["content-length"]);
print("Content Type:", headers["content-type"]);
}