send_bytes
This commit is contained in:
21
src/body.rs
21
src/body.rs
@@ -23,19 +23,19 @@ pub(crate) enum Payload {
|
|||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
JSON(SerdeValue),
|
JSON(SerdeValue),
|
||||||
Reader(Box<dyn Read + 'static>),
|
Reader(Box<dyn Read + 'static>),
|
||||||
|
Bytes(Vec<u8>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::fmt::Debug for Payload {
|
impl ::std::fmt::Debug for Payload {
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
|
||||||
let s = match self {
|
match self {
|
||||||
Payload::Empty => "Empty",
|
Payload::Empty => write!(f, "Empty"),
|
||||||
Payload::Text(t, _) => &t,
|
Payload::Text(t, _) => write!(f, "{}", t),
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
Payload::JSON(_) => "JSON",
|
Payload::JSON(_) => write!(f, "JSON"),
|
||||||
Payload::Reader(_) => "Reader",
|
Payload::Reader(_) => write!(f, "Reader"),
|
||||||
};
|
Payload::Bytes(v) => write!(f, "{:?}", v),
|
||||||
|
}
|
||||||
write!(f, "{}", s)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,6 +91,11 @@ impl Payload {
|
|||||||
SizedReader::new(Some(len), Box::new(cursor))
|
SizedReader::new(Some(len), Box::new(cursor))
|
||||||
}
|
}
|
||||||
Payload::Reader(read) => SizedReader::new(None, read),
|
Payload::Reader(read) => SizedReader::new(None, read),
|
||||||
|
Payload::Bytes(bytes) => {
|
||||||
|
let len = bytes.len();
|
||||||
|
let cursor = Cursor::new(bytes);
|
||||||
|
SizedReader::new(Some(len), Box::new(cursor))
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,6 +135,25 @@ impl Request {
|
|||||||
self.do_call(Payload::JSON(data))
|
self.do_call(Payload::JSON(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send data as bytes.
|
||||||
|
///
|
||||||
|
/// The `Content-Length` header is implicitly set to the length of the serialized value.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #[macro_use]
|
||||||
|
/// extern crate ureq;
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let body = b"Hello world!";
|
||||||
|
/// let r = ureq::post("/my_page")
|
||||||
|
/// .send_bytes(body);
|
||||||
|
/// println!("{:?}", r);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn send_bytes(&mut self, data: &[u8]) -> Response {
|
||||||
|
self.do_call(Payload::Bytes(data.to_owned()))
|
||||||
|
}
|
||||||
|
|
||||||
/// Send data as a string.
|
/// Send data as a string.
|
||||||
///
|
///
|
||||||
/// The `Content-Length` header is implicitly set to the length of the serialized value.
|
/// The `Content-Length` header is implicitly set to the length of the serialized value.
|
||||||
|
|||||||
Reference in New Issue
Block a user