pub struct Request { /* private fields */ }Expand description
A request for a ClientConn to send.
HTTP/1.1 requires a Host field; the connection refuses to send a
request without one. The request line always says HTTP/1.1.
§Examples
use http_net::Request;
let get = Request::get("/index.html").header("host", "example.com");
let post = Request::post("/api")
.header("host", "example.com")
.header("content-type", "application/json")
.body(br#"{"ok":true}"#.to_vec());
assert_eq!(get.method(), "GET");
assert_eq!(post.target(), "/api");Implementations§
Source§impl Request
impl Request
Sourcepub fn new(method: &str, target: &str) -> Self
pub fn new(method: &str, target: &str) -> Self
A request with method and target, validated when it is sent:
the method must be a token and the target must be in a form the
method allows (/path?query, an absolute URI, host:port for
CONNECT, or * for OPTIONS).
§Examples
let r = http_net::Request::new("PURGE", "/cache/a");
assert_eq!(r.method(), "PURGE");Sourcepub fn header(self, name: &str, value: impl AsRef<[u8]>) -> Self
pub fn header(self, name: &str, value: impl AsRef<[u8]>) -> Self
Adds a header field, validated as for Response::header. A second
host is refused with HttpError::InvalidHost.
§Examples
let r = http_net::Request::get("/").header("host", "a").header("accept", "*/*");
assert_eq!(r.header_value("accept"), Some(&b"*/*"[..]));Sourcepub fn body(self, body: impl Into<Cow<'static, [u8]>>) -> Self
pub fn body(self, body: impl Into<Cow<'static, [u8]>>) -> Self
Sets the whole body, sent with content-length.
§Examples
let r = http_net::Request::post("/").body(&b"x=1"[..]);
assert_eq!(r.body_bytes(), Some(&b"x=1"[..]));Sourcepub fn chunked(self) -> Self
pub fn chunked(self) -> Self
Streams the body with chunked encoding through
ClientConn::send_body and
ClientConn::end_body.
§Examples
assert_eq!(http_net::Request::post("/").chunked().body_bytes(), None);Sourcepub fn content_length(self, len: u64) -> Self
pub fn content_length(self, len: u64) -> Self
Streams a body of exactly len bytes.
§Examples
assert_eq!(http_net::Request::post("/").content_length(4).body_bytes(), None);Sourcepub fn close(self) -> Self
pub fn close(self) -> Self
Asks the server to close the connection after its response.
§Examples
let _ = http_net::Request::get("/").header("host", "a").close();Sourcepub fn upgrade(self, protocol: &str) -> Self
pub fn upgrade(self, protocol: &str) -> Self
Asks to switch the connection to protocol (writes
connection: upgrade and upgrade: <protocol>). A 101 answer
hands the connection over; see
ClientEvent::Upgraded.
§Examples
let r = http_net::Request::get("/rpc").header("host", "a").upgrade("websocket");
assert_eq!(r.header_value("upgrade"), Some(&b"websocket"[..]));Sourcepub fn body_bytes(&self) -> Option<&[u8]>
pub fn body_bytes(&self) -> Option<&[u8]>
The full body, or None for a streamed one.
§Examples
assert_eq!(http_net::Request::get("/").body_bytes(), Some(&[][..]));