#mizchi/js_web/http

    Fetch API for HTTP requests and responses.

    #Installation

    Add to your moon.pkg.json:

    { "import": [ "mizchi/js", "mizchi/js_web/http" ] }

    #Overview

    Provides bindings for the Fetch API, including Request, Response, Headers, and FormData.

    #Usage Example

    fn main {
    // Simple GET request
    let response = @http.fetch("https://api.example.com/data")

    // Create a Request
    let request = @http.Request::new("https://api.example.com/users")
    request.set_method("POST")
    request.set_header("Content-Type", "application/json")

    // Fetch with options
    let init = @http.RequestInit::new()
    init.set_method("POST")
    init.set_body("{\"name\":\"value\"}")
    let response2 = @http.fetch_with_init("https://api.example.com", init)

    // Working with Headers
    let headers = @http.Headers::new()
    headers.set("Content-Type", "application/json")
    headers.append("X-Custom-Header", "value")

    // Working with FormData
    let form = @http.FormData::new()
    form.append("field", "value")

    // Create Response
    let response3 = @http.Response::new("body content")
    }

    #Available Types

    • fetch() - Make HTTP requests
    • Request - HTTP request object
    • Response - HTTP response object
    • Headers - HTTP headers manipulation
    • FormData - Form data encoding

    #Reference

    FormData

    #external
    pub type FormData

    https://developer.mozilla.org/ja/docs/Web/API/FormData

    FormData::append

    fn FormData::append(self : FormData, name : String, value : String) -> Unit

    FormData::as_any

    FormData::delete

    fn FormData::delete(self : FormData, name : String) -> Unit

    FormData::get

    fn FormData::get(self : FormData, name : String) -> String

    Headers

    #external
    pub type Headers

    https://developer.mozilla.org/ja/docs/Web/API/Headers

    Headers::as_any

    fn Headers::as_any(self : Headers) ->
    Any

    Request

    pub(all) struct Request {
    bodyUsed : Bool
    url : String
    credentials : String
    }

    https://developer.mozilla.org/ja/docs/Web/API/Request

    Request::arraybuffer

    Request::as_any

    fn Request::as_any(self : Request) ->
    Any

    Request::blob

    async fn Request::blob(self : Request) ->
    Blob

    Request::clone

    fn Request::clone(self : Request) -> Request

    Request::json

    async fn Request::json(self : Request) ->
    Any

    Request::method_

    fn Request::method_(self : Request) -> String

    Get the HTTP method (e.g., "GET", "POST") Note: This is a method because "method" is a reserved word in MoonBit

    Request::new

    fn Request::new(url : String) -> Request

    https://developer.mozilla.org/ja/docs/Web/API/Request/Request

    Request::new_with_init

    fn Request::new_with_init(url : String, init : RequestInit) -> Request

    Create a new Request with init options

    Request::text

    async fn Request::text(self : Request) -> String

    RequestInit

    pub struct RequestInit {
    // private fields
    }

    RequestInit - options for creating a Request or fetch call https://developer.mozilla.org/en-US/docs/Web/API/RequestInit

    RequestInit::as_any

    RequestInit::get_body

    Get the body

    RequestInit::get_cache

    fn RequestInit::get_cache(self : RequestInit) -> String?

    Get the cache mode

    RequestInit::get_credentials

    fn RequestInit::get_credentials(self : RequestInit) -> String?

    Get the credentials

    RequestInit::get_headers

    fn RequestInit::get_headers(self : RequestInit) -> Headers?

    Get the headers

    RequestInit::get_integrity

    fn RequestInit::get_integrity(self : RequestInit) -> String?

    Get the integrity

    RequestInit::get_keepalive

    fn RequestInit::get_keepalive(self : RequestInit) -> Bool?

    Get the keepalive flag

    RequestInit::get_method

    fn RequestInit::get_method(self : RequestInit) -> String?

    Get the HTTP method

    RequestInit::get_mode

    fn RequestInit::get_mode(self : RequestInit) -> String?

    Get the mode

    RequestInit::get_priority

    fn RequestInit::get_priority(self : RequestInit) -> String?

    Get the priority

    RequestInit::get_redirect

    fn RequestInit::get_redirect(self : RequestInit) -> String?

    Get the redirect mode

    RequestInit::get_referrer

    fn RequestInit::get_referrer(self : RequestInit) -> String?

    Get the referrer

    RequestInit::get_referrerPolicy

    fn RequestInit::get_referrerPolicy(self : RequestInit) -> String?

    Get the referrer policy

    RequestInit::get_signal

    Get the abort signal

    RequestInit::new

    fn RequestInit::new(http_method? : String, headers? : Headers, body? :
    Any
    , mode? : String, credentials? : String, cache? : String, redirect? : String, referrer? : String, referrerPolicy? : String, integrity? : String, keepalive? : Bool, signal? :
    AbortSignal
    , priority? : String) -> RequestInit

    Response

    pub(all) struct Response {
    ok : Bool
    status : Int
    statusText : String
    redirected : Bool
    }

    https://developer.mozilla.org/ja/docs/Web/API/Response

    Response::as_any

    Response::body

    Get the body as a ReadableStream

    Response::clone

    fn Response::clone(self : Response) -> Response

    Response::error

    fn Response::error() -> Response

    https://developer.mozilla.org/ja/docs/Web/API/Response/error_static

    Response::formData

    #alias(form_data)
    async fn Response::formData(self : Response) -> FormData

    Response::html

    fn Response::html(body : String, status : Int) -> Response

    Create an HTML response

    Response::json

    async fn Response::json(self : Response) ->
    Any

    https://developer.mozilla.org/ja/docs/Web/API/Response/json

    Response::json_

    https://developer.mozilla.org/ja/docs/Web/API/Response/json_static

    Response::new

    fn Response::new(body? : String, status? : Int, statusText? : String, headers? :
    Any
    ) -> Response

    Create a new Response https://developer.mozilla.org/ja/docs/Web/API/Response/Response

    Response::new_with_body

    fn Response::new_with_body(body? :
    Any
    , status? : Int, statusText? : String, headers? :
    Any
    ) -> Response

    Create a new Response from an arbitrary BodyInit value — ArrayBuffer, TypedArray, Blob, ReadableStream, FormData, URLSearchParams, …. Response::new types the body as String for the common text case, which makes binary bodies unrepresentable (bytes cannot ride a UTF-16 String); this constructor mirrors RequestInit, whose body is already @core.Any. Typical use: serving image bytes from a Worker. https://developer.mozilla.org/ja/docs/Web/API/Response/Response

    Response::redirect

    fn Response::redirect(url : String, status? : Int) -> Response

    https://developer.mozilla.org/ja/docs/Web/API/Response/redirect_static

    Response::text

    async fn Response::text(self : Response) -> String

    https://developer.mozilla.org/ja/docs/Web/API/Response/text

    Response::text_

    fn Response::text_(body : String, status : Int) -> Response

    Create a text response

    Response::type_

    fn Response::type_(self : Response) -> String

    Get the response type Note: This is a method because "type" is a reserved word in MoonBit

    fetch

    async fn fetch(url : String, method_~ : String, headers? : Map[String, String], cache? : String, mode? : String, body? :
    Any
    , credentials? : String, integrity? : String, keepalive? : Bool, priority? : String, redirect? : String, referrer? : String, referrerPolicy? : String, signal? :
    AbortSignal
    ) -> Response

    https://developer.mozilla.org/ja/docs/Web/API/fetch

    fetch_request

    async fn fetch_request(request : Request) -> Response