如何在WebAssembly中使用Web-Sys与JSON主体提出发布请求?

发布于 2025-02-04 17:33:29 字数 830 浏览 3 评论 0原文

如何使用WebAssembly中的Web-Sys使用JSON主体创建发布请求?

下面的示例显示了如何提出请求,我需要更改 opts.Method(“ get”); opts.method(“ post”); ,但是如何我将json尸体传递到了重新界面。

    let mut opts = RequestInit::new();
    opts.method("GET");
    opts.credentials(web_sys::RequestCredentials::Include);
    
    let request = Request::new_with_str_and_init(
        "http://localhost:8080/api/v1/books",
         &opts
    ).unwrap();
    
    match web_sys::window() {
        Some(window) => {
            let _res = JsFuture::from(window.fetch_with_request(&request))
                .await
                .map(|err| web_sys::console::log_1(&format!("{:?}", err)
                .into()));
        },
        None => web_sys::console::log_1(&format!("window is none").into()),
    }

How to create POST request with JSON body using web-sys in WebAssembly?

This example below showing how to make GET request, I need to change opts.method("GET"); to opts.method("POST"); but how can i pass a JSON body to the reqeuest.

    let mut opts = RequestInit::new();
    opts.method("GET");
    opts.credentials(web_sys::RequestCredentials::Include);
    
    let request = Request::new_with_str_and_init(
        "http://localhost:8080/api/v1/books",
         &opts
    ).unwrap();
    
    match web_sys::window() {
        Some(window) => {
            let _res = JsFuture::from(window.fetch_with_request(&request))
                .await
                .map(|err| web_sys::console::log_1(&format!("{:?}", err)
                .into()));
        },
        None => web_sys::console::log_1(&format!("window is none").into()),
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2025-02-11 17:33:29

您可以使用 noreferrer“> repess unpessignit: :body() 以及使用 标题:: set 。您必须将选项< jsvalue>传递给request> requestInit :: body()。要通过字符串,您可以执行:

let mut opts = RequestInit::new();
opts.method("POST");
opts.body(Some(wasm_bindgen::JsValue::from_str("[1, 2, 3]")));
opts.credentials(web_sys::RequestCredentials::Include);
    
let request = Request::new_with_str_and_init(
    "http://localhost:8080/api/v1/books",
    &opts
).unwrap();

request.headers().set("content-type", "application/json");

// send the request

要发送更复杂的对象,可以使用 jsvalue :: from_serde

You can set the body using RequestInit::body() and the required headers using Headers::set. You have to pass an Option<JsValue> to RequestInit::body(). To pass a string, you can do:

let mut opts = RequestInit::new();
opts.method("POST");
opts.body(Some(wasm_bindgen::JsValue::from_str("[1, 2, 3]")));
opts.credentials(web_sys::RequestCredentials::Include);
    
let request = Request::new_with_str_and_init(
    "http://localhost:8080/api/v1/books",
    &opts
).unwrap();

request.headers().set("content-type", "application/json");

// send the request

To send a more complex object, you can use serde with JsValue::from_serde.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文