Add support for a HTTP body for POST (#139)
Closes https://github.com/emilk/egui/issues/137 Co-authored-by: Emil Ernerfeldt <emilernerfeldt@gmail.com>
This commit is contained in:
parent
1ac1a72fa8
commit
eedb63bb3b
3 changed files with 25 additions and 3 deletions
|
@ -3,9 +3,15 @@ pub use epi::http::{Request, Response};
|
||||||
/// NOTE: Ok(..) is returned on network error.
|
/// NOTE: Ok(..) is returned on network error.
|
||||||
/// Err is only for failure to use the fetch api.
|
/// Err is only for failure to use the fetch api.
|
||||||
pub fn fetch_blocking(request: &Request) -> Result<Response, String> {
|
pub fn fetch_blocking(request: &Request) -> Result<Response, String> {
|
||||||
let Request { method, url } = request;
|
let Request { method, url, body } = request;
|
||||||
|
|
||||||
let resp = ureq::request(method, url).set("Accept", "*/*").call();
|
let req = ureq::request(method, url).set("Accept", "*/*");
|
||||||
|
let resp = if body.is_empty() {
|
||||||
|
req.call()
|
||||||
|
} else {
|
||||||
|
req.set("Content-Type", "text/plain; charset=utf-8")
|
||||||
|
.send_string(body)
|
||||||
|
};
|
||||||
|
|
||||||
let (ok, resp) = match resp {
|
let (ok, resp) = match resp {
|
||||||
Ok(resp) => (true, resp),
|
Ok(resp) => (true, resp),
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub async fn fetch_async(request: &Request) -> Result<Response, String> {
|
||||||
/// NOTE: Ok(..) is returned on network error.
|
/// NOTE: Ok(..) is returned on network error.
|
||||||
/// Err is only for failure to use the fetch api.
|
/// Err is only for failure to use the fetch api.
|
||||||
async fn fetch_jsvalue(request: &Request) -> Result<Response, JsValue> {
|
async fn fetch_jsvalue(request: &Request) -> Result<Response, JsValue> {
|
||||||
let Request { method, url } = request;
|
let Request { method, url, body } = request;
|
||||||
|
|
||||||
// https://rustwasm.github.io/wasm-bindgen/examples/fetch.html
|
// https://rustwasm.github.io/wasm-bindgen/examples/fetch.html
|
||||||
|
|
||||||
|
@ -24,6 +24,10 @@ async fn fetch_jsvalue(request: &Request) -> Result<Response, JsValue> {
|
||||||
opts.method(method);
|
opts.method(method);
|
||||||
opts.mode(web_sys::RequestMode::Cors);
|
opts.mode(web_sys::RequestMode::Cors);
|
||||||
|
|
||||||
|
if !body.is_empty() {
|
||||||
|
opts.body(Some(&JsValue::from_str(body)));
|
||||||
|
}
|
||||||
|
|
||||||
let request = web_sys::Request::new_with_str_and_init(&url, &opts)?;
|
let request = web_sys::Request::new_with_str_and_init(&url, &opts)?;
|
||||||
request.headers().set("Accept", "*/*")?;
|
request.headers().set("Accept", "*/*")?;
|
||||||
|
|
||||||
|
|
|
@ -263,6 +263,8 @@ pub mod http {
|
||||||
pub method: String,
|
pub method: String,
|
||||||
/// https://…
|
/// https://…
|
||||||
pub url: String,
|
pub url: String,
|
||||||
|
/// x-www-form-urlencoded body
|
||||||
|
pub body: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
|
@ -271,6 +273,16 @@ pub mod http {
|
||||||
Self {
|
Self {
|
||||||
method: "GET".to_owned(),
|
method: "GET".to_owned(),
|
||||||
url: url.into(),
|
url: url.into(),
|
||||||
|
body: "".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a `POST` requests with the give url and body.
|
||||||
|
pub fn post(url: impl Into<String>, body: impl Into<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
method: "POST".to_owned(),
|
||||||
|
url: url.into(),
|
||||||
|
body: body.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue