wasm: Omit request body if it's empty (#1012)

This should allow creating GET and HEAD requests from http::Request
This commit is contained in:
Jonas Platte
2020-08-21 00:42:48 +02:00
committed by GitHub
parent 9e23103371
commit d42385e7f2
3 changed files with 16 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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,