如何在 WebAssembly 中使用 web-sys 创建带有 JSON 主体的 POST 请求?
下面这个例子展示了如何发出 GET 请求,我需要更改opts.method("GET"); 到opts.method("POST"); 但我如何将 JSON 主体传递给 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()),
}
Run Code Online (Sandbox Code Playgroud)