NetAdded in v6.0
The net
module provides an interface for performing network-related operations directly from EdgeDB. It is useful for integrating with external services, fetching data from APIs, or triggering webhooks as part of your database logic.
net::RequestState (added in 6.0) |
An enum representing the state of a network request. |
net::RequestFailureKind (added in 6.0) |
An enum representing the kind of failure that occurred for a network request. |
net::http::Method (added in 6.0) |
An enum representing HTTP methods. |
net::http::Response (added in 6.0) |
A type representing an HTTP response. |
net::http::ScheduledRequest (added in 6.0) |
A type representing a scheduled HTTP request. |
HTTP Submodule
The net::http
submodule provides types and functions for making HTTP requests.
Overview
The primary function for scheduling HTTP requests is net::http::schedule_request()
(added in 6.0). This function lets you specify the URL, HTTP method, headers, and body of the request. Once scheduled, you can monitor the request’s status and process its response when available.
Example Usage
Here’s a simple example of how to use the net::http
module to make a GET request:
with request := (
net::http::schedule_request(
'https://example.com',
method := net::http::Method.POST,
headers := [('Content-Type', 'application/json')],
body := <bytes>$${"key": "value"}$$
)
)
select request.id;
This ID will be helpful if you need to observe a request’s response. You can poll the ScheduledRequest
object in order to get any response data or failure information:
-
Check the State: Use the
state
field to determine the current status of the request. -
Handle Failures: If the request has failed, inspect the
failure
field to understand the kind of failure (e.g.,NetworkError
orTimeout
) and any associated message. -
Process the Response: If the request is completed successfully, access the
response.body
to retrieve the data returned by the request. The body is inbytes
format and may need conversion or parsing.
In the following example, we’ll query the ScheduledRequest
object we created above using the ID we selected. Once the request is completed or it has failed, this query will return the response data or the failure information:
with
request := <std::net::http::ScheduledRequest><uuid>$request_id,
select request {
state,
failure,
response: {
status,
headers,
body,
},
} filter .state in {net::RequestState.Failed, net::RequestState.Completed};
Reference
A type representing an HTTP response.
- created_at -> datetime
-
The timestamp when the response was created.
- status -> int16
-
The HTTP status code of the response.
- headers -> array<tuple<name: str, value: str>>
-
The headers of the response.
- body -> bytes
-
The body of the response.
A type representing a scheduled HTTP request.
- state -> net::RequestState
-
The current state of the request.
- created_at -> datetime
-
The timestamp when the request was created.
- failure -> tuple<kind: net::RequestFailureKind, message: str>
-
Information about the failure, if the request failed.
- url -> str
-
The URL of the request.
- method -> net::http::Method
-
The HTTP method of the request.
- headers -> array<tuple<name: str, value: str>>
-
The headers of the request.
- body -> bytes
-
The body of the request.
- response -> net::http::Response
-
The response to the request, if completed.
Schedules an HTTP request.
Parameters:
-
url
: The URL to send the request to. -
body
: The body of the request (optional). -
method
: The HTTP method to use (optional, defaults to GET). -
headers
: The headers to include in the request (optional).
Returns net::http::ScheduledRequest
object representing
the scheduled request.
Example:
SELECT net::http::schedule_request(
'https://example.com',
method := net::http::Method.POST,
headers := [('Content-Type', 'application/json')],
body := <bytes>$${"key": "value"}$$
);