Skip to content

Commit

Permalink
wasm: Omit request body if it's empty (seanmonstar#1012)
Browse files Browse the repository at this point in the history
This should allow creating GET and HEAD requests from http::Request
  • Loading branch information
jplatte authored Aug 20, 2020
1 parent 9e23103 commit d42385e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/wasm/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ impl Body {
inner: Inner::Multipart(f),
}
}

pub(crate) fn is_empty(&self) -> bool {
match &self.inner {
Inner::Bytes(bytes) => bytes.is_empty(),
Inner::Multipart(form) => form.is_empty(),
}
}
}

impl From<Bytes> for Body {
Expand Down
4 changes: 3 additions & 1 deletion src/wasm/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ async fn fetch(req: Request) -> crate::Result<Response> {
}

if let Some(body) = req.body() {
init.body(Some(&body.to_js_value()?.as_ref().as_ref()));
if !body.is_empty() {
init.body(Some(&body.to_js_value()?.as_ref().as_ref()));
}
}

let js_req = web_sys::Request::new_with_str_and_init(req.url().as_str(), &init)
Expand Down
6 changes: 6 additions & 0 deletions src/wasm/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub struct Form {
inner: FormParts<Part>,
}

impl Form {
pub(crate) fn is_empty(&self) -> bool {
self.inner.fields.is_empty()
}
}

/// A field in a multipart form.
pub struct Part {
meta: PartMetadata,
Expand Down

0 comments on commit d42385e

Please sign in to comment.