Struct edjx::fetch::HttpFetch

source ·
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§

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

Creates a request with the GET method and URI, no headers, and an empty body.

Creates a request with the POST method and URI, no headers, and an empty body.

Creates a request with the PUT method and URI, no headers, and an empty body.

Creates a request with the DELETE method and URI, no headers, and an empty body.

Sets the HTTP version of the request.

Sets the HTTP version of the request.

Sets the given &str value as the body of the request.

Any body that was previously set on the request is discarded.

Sets the given Vec<u8> value as the body of the request.

Any body that was previously set on the request is discarded.

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"));

Returns the request’s header map as http::header::HeaderMap.

Returns the request’s header map as a mutable reference to http::header::HeaderMap.

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();

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();

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

Trait Implementations§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.