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        /// The headers for the request.
 9        headers: list<tuple<string, string>>,
10        /// The request body.
11        body: option<list<u8>>,
12        /// The policy to use for redirects.
13        redirect-policy: redirect-policy,
14    }
15
16    /// HTTP methods.
17    enum http-method {
18        /// `GET`
19        get,
20        /// `HEAD`
21        head,
22        /// `POST`
23        post,
24        /// `PUT`
25        put,
26        /// `DELETE`
27        delete,
28        /// `OPTIONS`
29        options,
30        /// `PATCH`
31        patch,
32    }
33
34    /// The policy for dealing with redirects received from the server.
35    variant redirect-policy {
36        /// Redirects from the server will not be followed.
37        ///
38        /// This is the default behavior.
39        no-follow,
40        /// Redirects from the server will be followed up to the specified limit.
41        follow-limit(u32),
42        /// All redirects from the server will be followed.
43        follow-all,
44    }
45
46    /// An HTTP response.
47    record http-response {
48        /// The response headers.
49        headers: list<tuple<string, string>>,
50        /// The response body.
51        body: list<u8>,
52    }
53
54    /// An HTTP response that includes the status code.
55    ///
56    /// Used by `fetch-fallible` which returns responses for all status codes
57    /// rather than treating some status codes as errors.
58    record http-response-with-status {
59        /// The HTTP status code.
60        status: u16,
61        /// The response headers.
62        headers: list<tuple<string, string>>,
63        /// The response body.
64        body: list<u8>,
65    }
66
67    /// Performs an HTTP request and returns the response.
68    /// Returns an error if the response status is 4xx or 5xx.
69    fetch: func(req: http-request) -> result<http-response, string>;
70
71    /// Performs an HTTP request and returns the response regardless of its status code.
72    fetch-fallible: func(req: http-request) -> result<http-response-with-status, string>;
73
74    /// An HTTP response stream.
75    resource http-response-stream {
76        /// Retrieves the next chunk of data from the response stream.
77        ///
78        /// Returns `Ok(None)` if the stream has ended.
79        next-chunk: func() -> result<option<list<u8>>, string>;
80    }
81
82    /// Performs an HTTP request and returns a response stream.
83    fetch-stream: func(req: http-request) -> result<http-response-stream, string>;
84}