http-client.wit

 1interface http-client {
 2    /// An HTTP request.
 3    record http-request {
 4        /// The HTTP method for the request.
 5        method: http-method,
 6        /// The URL to which the request should be made.
 7        url: string,
 8        /// Headers for the request.
 9        headers: list<tuple<string, string>>,
10        /// The request body.
11        body: option<list<u8>>,
12    }
13
14    /// HTTP methods.
15    enum http-method {
16        get,
17        post,
18        put,
19        delete,
20        head,
21        options,
22        patch,
23    }
24
25    /// An HTTP response.
26    record http-response {
27        /// The response headers.
28        headers: list<tuple<string, string>>,
29        /// The response body.
30        body: list<u8>,
31    }
32
33    /// Performs an HTTP request and returns the response.
34    fetch: func(req: http-request) -> result<http-response, string>;
35
36    /// An HTTP response stream.
37    resource http-response-stream {
38        /// Retrieves the next chunk of data from the response stream.
39        /// Returns None if the stream has ended.
40        next-chunk: func() -> result<option<list<u8>>, string>;
41    }
42
43    /// Performs an HTTP request and returns a response stream.
44    fetch-stream: func(req: http-request) -> result<http-response-stream, string>;
45}