wasm: Add request body in the form of Bytes (#696)

* Add body bytes

* Add example and header creation code
This commit is contained in:
John Gallagher
2019-11-04 12:17:05 -05:00
committed by Sean McArthur
parent b24b0be461
commit f6f81f9cc1
11 changed files with 6306 additions and 9 deletions

View File

@@ -1,9 +1,11 @@
use http::HttpTryFrom;
use std::fmt;
use http::{Method, HeaderMap};
use http::Method;
use url::Url;
use super::{Body, Client, Response};
use crate::header::{HeaderMap, HeaderName, HeaderValue};
/// A request which can be executed with `Client::execute()`.
pub struct Request {
@@ -83,7 +85,6 @@ impl RequestBuilder {
RequestBuilder { client, request }
}
/// Set the request body.
pub fn body<T: Into<Body>>(mut self, body: T) -> RequestBuilder {
if let Ok(ref mut req) = self.request {
@@ -92,6 +93,30 @@ impl RequestBuilder {
self
}
/// Add a `Header` to this Request.
pub fn header<K, V>(mut self, key: K, value: V) -> RequestBuilder
where
HeaderName: HttpTryFrom<K>,
HeaderValue: HttpTryFrom<V>,
{
let mut error = None;
if let Ok(ref mut req) = self.request {
match <HeaderName as HttpTryFrom<K>>::try_from(key) {
Ok(key) => match <HeaderValue as HttpTryFrom<V>>::try_from(value) {
Ok(value) => {
req.headers_mut().append(key, value);
}
Err(e) => error = Some(crate::error::builder(e.into())),
},
Err(e) => error = Some(crate::error::builder(e.into())),
};
}
if let Some(err) = error {
self.request = Err(err);
}
self
}
/// Constructs the Request and sends it to the target URL, returning a
/// future Response.
///