///|
#cfg(any(target="native", target="js"))
async test {
let (response, body) = @http.get_text("https://www.moonbitlang.com")
inspect(response.code, content="200")
assert_true(body.has_prefix("<!doctype html>"))
}///|
#cfg(any(target="native", target="js"))
async test {
let (response, conn) = @http.get("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, url : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> (ResponseHeader, ClientConnection)async fn Client::get_binary(self : Client, uri : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> (ResponseHeader, Bytes)async fn Client::get_json(self : Client, uri : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> (ResponseHeader, Json)async fn Client::get_text(self : Client, uri : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> (ResponseHeader, String)async fn Client::post(self : Client, url : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> ClientConnectionasync fn Client::put(self : Client, url : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> ClientConnectionasync fn Client::request(self : Client, meth : RequestMethod, url : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> ClientConnectionimpl BinaryReader for ClientConnectionimpl BinaryWriter for ClientConnection#alias(new, deprecated="`new` is deprecated, use `ClientConnection` instead")
async fn ClientConnection::ClientConnection(uri : String, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection, trust? : TrustedRoot) -> ClientConnectionasync fn ClientConnection::end_request(self : ClientConnection, on_informational_response? : async (ResponseHeader) -> Unit) -> ResponseHeaderasync fn ClientConnection::peek_until(self : ClientConnection, pattern : BytesView, include_delimiter? : Bool) -> BytesView?async fn ClientConnection::read_into(self : ClientConnection, dst : FixedArray[Byte], offset? : Int, max_len? : Int) -> Intasync fn ClientConnection::read_until(self : ClientConnection, pattern : BytesView, include_delimiter? : Bool) -> Bytes?async fn ClientConnection::request(self : ClientConnection, meth : RequestMethod, path : StringView, extra_headers? : Map[CaseInsensitiveString, String]) -> Unitpub(all) struct RequestHeader {
meth : RequestMethod
path : String
headers : Map[CaseInsensitiveString, String]
} derive(Debug, ToJson)#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 ServerConnectionfn ServerConnection::new(conn : Tcp, headers? : Map[CaseInsensitiveString, String]) -> ServerConnectionasync fn ServerConnection::peek_until(self : ServerConnection, pattern : BytesView, include_delimiter? : Bool) -> BytesView?async fn ServerConnection::read_into(self : ServerConnection, dst : FixedArray[Byte], offset? : Int, max_len? : Int) -> Intasync fn ServerConnection::read_until(self : ServerConnection, pattern : BytesView, include_delimiter? : Bool) -> Bytes?async fn ServerConnection::send_response(self : ServerConnection, code : Int, reason : String, extra_headers? : Map[CaseInsensitiveString, String], cookies? : Array[Cookie]) -> Unitasync fn[X : Show] ServerConnection::write_string_interpolation(self : ServerConnection, data : X) -> Unitasync fn get(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, ClientConnection)async fn get_binary(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, Bytes)async fn get_json(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, Json)async fn get_text(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, String)async fn post(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> ClientConnectionasync fn post_binary(uri : StringView, content : BytesView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, Bytes)async fn post_json(uri : StringView, content : Json, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, Json)async fn post_text(uri : StringView, content : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, String)async fn put(uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> ClientConnectionasync fn put_binary(uri : StringView, content : BytesView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, Bytes)async fn put_json(uri : StringView, content : Json, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, Json)async fn put_text(uri : StringView, content : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, String)async fn request(meth : RequestMethod, uri : StringView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> ClientConnectionasync fn request_binary(meth : RequestMethod, uri : StringView, body : BytesView, headers? : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, Bytes)async fn request_json(meth : RequestMethod, uri : StringView, body : Json?, headers~ : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, Json)async fn request_text(meth : RequestMethod, uri : StringView, body : StringView, headers~ : Map[CaseInsensitiveString, String], proxy? : ClientConnection) -> (ResponseHeader, String)Install
Installed by default