///|
#cfg(any(target="native", target="js"))
async test {
let (response, body) = @http.get("https://www.moonbitlang.com")
inspect(response.code, content="200")
assert_true(body.text().has_prefix("<!doctype html>"))
}///|
#cfg(any(target="native", target="js"))
async test {
let (response, conn) = @http.get_stream("https://www.moonbitlang.com")
defer conn.close()
inspect(response.code, content="200")
let body = conn.text.read_all()
assert_true(body.has_prefix("<!doctype html>"))
}note: currently the @http.Client is just a placeholder that create a new connection for every request. Proper connection pool support will be implemented in the near future.
///|
/// A simple HTTP server that return 404 for every request
#cfg(target="native")
pub async fn server(listen_addr : @socket.Addr) -> Unit {
@http.Server(listen_addr).run_forever((request, conn) => {
conn.send_response(404, "NotFound")
conn.text.write("`\{request.path}` not found")
})
}impl Compare for CaseInsensitiveStringimpl Eq for CaseInsensitiveStringimpl Hash for CaseInsensitiveStringimpl Show for CaseInsensitiveStringimpl Debug for CaseInsensitiveStringtype Clientasync fn Client::get(self : Client, uri : StringView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::get_stream(self : Client, url : StringView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, ClientConnection)async fn Client::post_binary(self : Client, uri : StringView, content : BytesView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::post_json(self : Client, uri : StringView, content : Json, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::post_stream(self : Client, url : StringView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> ClientConnectionasync fn Client::post_text(self : Client, uri : StringView, content : StringView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::put_binary(self : Client, uri : StringView, content : BytesView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::put_json(self : Client, uri : StringView, content : Json, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::put_stream(self : Client, url : StringView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> ClientConnectionasync fn Client::put_text(self : Client, uri : StringView, content : StringView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::request_binary(self : Client, meth : RequestMethod, url : StringView, body : BytesView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::request_json(self : Client, meth : RequestMethod, url : StringView, body : Json, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn Client::request_stream(self : Client, meth : RequestMethod, url : StringView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> ClientConnectionasync fn Client::request_text(self : Client, meth : RequestMethod, url : StringView, body : StringView, extra_headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)impl BinaryReader for ClientConnectionimpl BinaryWriter for ClientConnection#alias(new, deprecated="`new` is deprecated, use `ClientConnection` instead")
async fn ClientConnection::ClientConnection(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy, trust? : TrustedRoot) -> ClientConnectionasync fn ClientConnection::drop_exactly(self : ClientConnection, len : Int) -> Unit raise ReaderErrorasync fn ClientConnection::end_request(self : ClientConnection, on_informational_response? : async (ResponseHeader) -> Unit) -> ResponseHeaderasync fn ClientConnection::peek_at_least(self : ClientConnection, min_len : Int) -> BytesView raise ReaderErrorasync fn ClientConnection::peek_exactly(self : ClientConnection, len : Int) -> BytesView raise ReaderErrorasync fn ClientConnection::peek_until(self : ClientConnection, pattern : BytesView, include_delimiter? : Bool) -> BytesView? raise ReaderErrorasync fn ClientConnection::read_at_least(self : ClientConnection, min_len : Int) -> Bytes raise ReaderErrorasync fn ClientConnection::read_exactly(self : ClientConnection, len : Int) -> Bytes raise ReaderErrorasync fn ClientConnection::read_into(self : ClientConnection, dst : MutArrayView[Byte]) -> Int raise ReaderErrorasync fn ClientConnection::read_until(self : ClientConnection, pattern : BytesView, include_delimiter? : Bool) -> Bytes? raise ReaderErrorasync fn ClientConnection::request(self : ClientConnection, meth : RequestMethod, path : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> Unitasync fn ClientConnection::write(self : ClientConnection, data : BytesView) -> Unit raise WriterErrortype MessageBodytype Proxypub struct RequestHeader {
meth : RequestMethod
path : String
headers : Map[CaseInsensitiveString, String]
} derive(Debug, ToJson)impl Show for RequestMethodtype Server#alias(new, deprecated="`new` is deprecated, use `Server` instead")
async fn Server::Server(addr : Addr, dual_stack? : Bool, reuse_addr? : Bool, headers? : Map[CaseInsensitiveString, String]) -> Serverasync fn Server::run_forever(self : Server, f : async (RequestHeader, ServerConnection) -> Unit, allow_failure? : Bool, max_connections? : Int) -> Unitimpl BinaryReader for ServerConnectionimpl BinaryWriter for ServerConnection#alias(new, deprecated="Use `ServerConnection(conn, headers=...)` instead")
fn ServerConnection::ServerConnection(conn : Tcp, headers? : Map[CaseInsensitiveString, String]) -> ServerConnectionasync fn ServerConnection::drop_exactly(self : ServerConnection, len : Int) -> Unit raise ReaderErrorasync fn ServerConnection::peek_at_least(self : ServerConnection, min_len : Int) -> BytesView raise ReaderErrorasync fn ServerConnection::peek_exactly(self : ServerConnection, len : Int) -> BytesView raise ReaderErrorasync fn ServerConnection::peek_until(self : ServerConnection, pattern : BytesView, include_delimiter? : Bool) -> BytesView? raise ReaderErrorasync fn ServerConnection::read_at_least(self : ServerConnection, min_len : Int) -> Bytes raise ReaderErrorasync fn ServerConnection::read_exactly(self : ServerConnection, len : Int) -> Bytes raise ReaderErrorasync fn ServerConnection::read_into(self : ServerConnection, dst : MutArrayView[Byte]) -> Int raise ReaderErrorasync fn ServerConnection::read_until(self : ServerConnection, pattern : BytesView, include_delimiter? : Bool) -> Bytes? raise ReaderErrorasync fn ServerConnection::send_response(self : ServerConnection, code : Int, reason : String, extra_headers? : Map[CaseInsensitiveString, String], cookies? : Array[Cookie]) -> Unitasync fn ServerConnection::write(self : ServerConnection, data : BytesView) -> Unit raise WriterErrorasync fn get(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn get_stream(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, ClientConnection)async fn post_binary(uri : StringView, content : BytesView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn post_json(uri : StringView, content : Json, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn post_stream(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> ClientConnectionasync fn post_text(uri : StringView, content : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn put_binary(uri : StringView, content : BytesView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn put_json(uri : StringView, content : Json, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn put_stream(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> ClientConnectionasync fn put_text(uri : StringView, content : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn request_binary(meth : RequestMethod, uri : StringView, body : BytesView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn request_json(meth : RequestMethod, uri : StringView, body : Json, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)async fn request_stream(meth : RequestMethod, uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> ClientConnectionasync fn request_text(meth : RequestMethod, uri : StringView, body : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : Proxy) -> (ResponseHeader, MessageBody)Install
Installed by default