pub struct HttpFetch { /* private fields */ }
Expand description
An HTTP fetch request, including body, headers, method, and URL.
Creation, conversion, and sending
New requests with an empty body can be created programmatically using HttpFetch::new()
. In
addition, there are convenience constructors such as HttpFetch::get()
, HttpFetch::post()
,
HttpFetch::put()
, HttpFetch::delete()
which automatically select the appropriate method.
Send requests using HttpFetch::send()
, for Streaming use HttpFetch::send_streaming()
For interoperability with other Rust libraries, HttpFetch
can be initiated using
http
, which is crate’s http::Request
type along with the HttpFetch::send_using_standard_http_lib()
function
passing Request<Option<Bytes>>
as parameters.
Builder-style Methods
Request
can be used as a
builder, allowing requests to
be constructed and used through method chaining. Methods with the with_
and set_
name prefix, such as
set_header()
, return Self
to allow chaining. The builder style is
typically most useful when constructing and using a request in a single expression.
For example:
use edjx::{HttpFetch, Request, HttpError};
HttpFetch::get("https://example.com")
.set_header("my-header", "hello!")
.with_raw_body("empty body")
.send()?;
Implementations§
source§impl HttpFetch
impl HttpFetch
sourcepub fn new(uri: Uri, m: Method) -> HttpFetch
pub fn new(uri: Uri, m: Method) -> HttpFetch
Creates a request with the given method and URI, no headers, and an empty body.
Example
use edjx::{HttpFetch, Request, HttpError, Method};
let fetch_uri = Uri::from_str("https://httpbin.org/get").unwrap();
let request = HttpFetch::new(fetch_uri, Method::GET);
sourcepub fn get(uri: Uri) -> HttpFetch
pub fn get(uri: Uri) -> HttpFetch
Creates a request with the GET method and URI, no headers, and an empty body.
sourcepub fn post(uri: Uri) -> HttpFetch
pub fn post(uri: Uri) -> HttpFetch
Creates a request with the POST method and URI, no headers, and an empty body.
sourcepub fn put(uri: Uri) -> HttpFetch
pub fn put(uri: Uri) -> HttpFetch
Creates a request with the PUT method and URI, no headers, and an empty body.
sourcepub fn delete(uri: Uri) -> HttpFetch
pub fn delete(uri: Uri) -> HttpFetch
Creates a request with the DELETE method and URI, no headers, and an empty body.
sourcepub fn set_version(self, version: Version) -> Self
pub fn set_version(self, version: Version) -> Self
Sets the HTTP version of the request.
sourcepub fn get_version(&self) -> Version
pub fn get_version(&self) -> Version
Sets the HTTP version of the request.
sourcepub fn with_raw_body(self, text: &str) -> Self
pub fn with_raw_body(self, text: &str) -> Self
Sets the given &str
value as the body of the request.
Any body that was previously set on the request is discarded.
sourcepub fn with_binary_body(self, bytes: Vec<u8>) -> Self
pub fn with_binary_body(self, bytes: Vec<u8>) -> Self
Sets the given Vec<u8>
value as the body of the request.
Any body that was previously set on the request is discarded.
sourcepub fn set_header(self, header_name: HeaderName, value: HeaderValue) -> Self
pub fn set_header(self, header_name: HeaderName, value: HeaderValue) -> Self
Sets a request header to the given value, discarding any previous values for the given header name.
Example
use edjx::HttpFetch;
let fetch_uri = Uri::from_str("https://httpbin.org/get").unwrap();
let mut resp = HttpFetch::get(fetch_uri).set_header("hello".parse().unwrap(), "world!".parse().unwrap());
let header_map = resp.get_headers();
assert!(header_map.contains_key("hello"));
sourcepub fn get_headers(&self) -> &HeaderMap
pub fn get_headers(&self) -> &HeaderMap
Returns the request’s header map as http::header::HeaderMap
.
sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns the request’s header map as a mutable reference to http::header::HeaderMap
.
sourcepub fn send(&self) -> Result<FetchResponse, HttpError>
pub fn send(&self) -> Result<FetchResponse, HttpError>
Sends the request to the server, and returns after the response is received or an error occurs.
Error Response
This method returns an error response of kind HttpError
if any of the configured limits
are exceeded.
Example
Sending the client request:
use edjx::HttpFetch;
HttpFetch::get("https://example.com").send();
sourcepub fn send_streaming(
&self
) -> Result<(FetchResponsePending, WriteStream), HttpError>
pub fn send_streaming(
&self
) -> Result<(FetchResponsePending, WriteStream), HttpError>
Starts streaming an HTTP Fetch request to the server.
This method returns a FetchResponsePending
object and a WriteStream
object.
The WriteStream
object is used to stream data, the FetchResponsePending
object
is a placeholder to retreive FetchResponse
. In the background,
this method triggers sending HTTP headers and other HTTP artifacts
to the server.
This method and the send()
method cannot be used together.
Error Response
This method returns an error response of kind HttpError
Example
Streaming the client request:
use edjx::{HttpFetch, FetchResponse, BaseStream, Uri};
use std::str::FromStr;
let fetch_uri = Uri::from_str("https://httpbin.org/post").unwrap();
let (fet_resp_pending, mut write_stream) = HttpFetch::post(fetch_uri).send_streaming().unwrap();
write_stream.write_chunk_text("Chunk with text").unwrap();
write_stream.write_chunk_binary(Vec::from("Chunk with binary data")).unwrap();
write_stream.close().unwrap();
let mut fetch_res: FetchResponse = fet_resp_pending.get_fetch_response().unwrap();
sourcepub fn send_using_standard_http_lib(
req: Request<Option<Bytes>>
) -> Result<FetchResponse, HttpError>
pub fn send_using_standard_http_lib(
req: Request<Option<Bytes>>
) -> Result<FetchResponse, HttpError>
Sends the request to the server, using standard HTTP request library http::Request
and
returns after a response is received or an error occurs.
Error Response
This method returns an error response of kind HttpError
if any of the
configured limits are exceeded.
Example
Sending a request using http::Request
:
use edjx::HttpFetch;
use http::Request;
use bytes::Bytes;
let req_builder = http::request::Builder::new()
.method(http::Method::POST)
.uri(&query_url)
.header("header1", "header1_value");
let bytes = Bytes::from("Body bytes");
let req = req_builder.body(Some(bytes)).unwrap();
//let req = req_builder.body(None).unwrap(); //for request without body.
let fetch_response = HttpFetch::send_using_standard_http_lib(req);