Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a HTTP body #139

Merged
merged 7 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion egui_web/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn fetch_async(request: &Request) -> Result<Response, String> {
/// NOTE: Ok(..) is returned on network error.
/// Err is only for failure to use the fetch api.
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

Expand All @@ -24,6 +24,10 @@ async fn fetch_jsvalue(request: &Request) -> Result<Response, JsValue> {
opts.method(method);
opts.mode(web_sys::RequestMode::Cors);

if method == &"POST".to_string() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method == "POST" should work fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested manually with my personal development http server. I use kore from kore.io.
By default the Content-Type is plain/text. If we would send binary data then we need to change it, but since web_sys only has the body referenced by JsValue (and I am new to Rust - less than a week) I believe that we need only to send it if we want to change.

I the GLIUM version since it uses ureq it has the separate functions to send binary, text, form or json.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested manually with my personal development http server.

Great! Web or glium version or both?

I am new to Rust - less than a week

Wow! Good work on this PR, and welcome to the wonderful world of Rust!

PauloMelo marked this conversation as resolved.
Show resolved Hide resolved
opts.body(Some(&JsValue::from_serde(body).unwrap()));
emilk marked this conversation as resolved.
Show resolved Hide resolved
}

let request = web_sys::Request::new_with_str_and_init(&url, &opts)?;
request.headers().set("Accept", "*/*")?;

Expand Down
12 changes: 12 additions & 0 deletions epi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ pub mod http {
pub method: String,
/// https://…
pub url: String,
/// x-www-form-urlencoded body
pub body: String,
}

impl Request {
Expand All @@ -271,6 +273,16 @@ pub mod http {
Self {
method: "GET".to_owned(),
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(),
}
}
}
Expand Down