pub struct Request { /* private fields */ }Expand description
An outgoing HTTP request, built step by step and sent with send.
§Examples
use ocre::http::Request;
use serde_json::json;
let request = Request::post("https://api.example.com/v1/jobs")
.bearer("rk_123")
.header("Idempotency-Key", "job-42")
.json(&json!({"input": {"video": "a.mp4"}}));
assert_eq!(request.method(), "POST");
assert_eq!(request.headers(), [
("authorization".to_owned(), "Bearer rk_123".to_owned()),
("idempotency-key".to_owned(), "job-42".to_owned()),
("content-type".to_owned(), "application/json".to_owned()),
]);
assert_eq!(request.body_bytes(), Some(br#"{"input":{"video":"a.mp4"}}"#.as_slice()));Implementations§
Source§impl Request
impl Request
Sourcepub fn new(method: Method, url: impl Into<String>) -> Self
pub fn new(method: Method, url: impl Into<String>) -> Self
A request of method to url (absolute, https:// or http://).
§Examples
use axum::http::Method;
let request = ocre::http::Request::new(Method::OPTIONS, "https://example.com/");
assert_eq!((request.method(), request.url()), ("OPTIONS", "https://example.com/"));Sourcepub fn get(url: impl Into<String>) -> Self
pub fn get(url: impl Into<String>) -> Self
A GET of url.
§Examples
assert_eq!(ocre::http::Request::get("https://example.com").method(), "GET");Sourcepub fn post(url: impl Into<String>) -> Self
pub fn post(url: impl Into<String>) -> Self
A POST to url.
§Examples
assert_eq!(ocre::http::Request::post("https://example.com").method(), "POST");Sourcepub fn put(url: impl Into<String>) -> Self
pub fn put(url: impl Into<String>) -> Self
A PUT to url.
§Examples
assert_eq!(ocre::http::Request::put("https://example.com").method(), "PUT");Sourcepub fn patch(url: impl Into<String>) -> Self
pub fn patch(url: impl Into<String>) -> Self
A PATCH to url.
§Examples
assert_eq!(ocre::http::Request::patch("https://example.com").method(), "PATCH");Sourcepub fn delete(url: impl Into<String>) -> Self
pub fn delete(url: impl Into<String>) -> Self
A DELETE of url.
§Examples
assert_eq!(ocre::http::Request::delete("https://example.com").method(), "DELETE");Sourcepub fn head(url: impl Into<String>) -> Self
pub fn head(url: impl Into<String>) -> Self
A HEAD of url.
§Examples
assert_eq!(ocre::http::Request::head("https://example.com").method(), "HEAD");Sourcepub fn header(self, name: &str, value: &str) -> Self
pub fn header(self, name: &str, value: &str) -> Self
Adds a header (the name is lowercased; a header set twice is sent twice).
§Examples
let request = ocre::http::Request::get("https://example.com").header("Accept", "text/csv");
assert_eq!(request.headers(), [("accept".to_owned(), "text/csv".to_owned())]);Sourcepub fn bearer(self, token: &str) -> Self
pub fn bearer(self, token: &str) -> Self
Adds Authorization: Bearer <token>.
§Examples
let request = ocre::http::Request::get("https://example.com").bearer("t0k");
assert_eq!(request.headers(), [("authorization".to_owned(), "Bearer t0k".to_owned())]);Sourcepub fn query(self, pairs: &[(&str, &str)]) -> Self
pub fn query(self, pairs: &[(&str, &str)]) -> Self
Appends query parameters to the URL, percent-encoded (percent_encode).
§Examples
let request = ocre::http::Request::get("https://example.com/search?page=2").query(&[("q", "a&b"), ("lang", "fr")]);
assert_eq!(request.url(), "https://example.com/search?page=2&q=a%26b&lang=fr");Sourcepub fn json(self, value: &impl Serialize) -> Self
pub fn json(self, value: &impl Serialize) -> Self
Sends value as JSON (Content-Type: application/json).
A value that cannot be serialized (a map with non-string keys) makes
send fail without sending anything.
§Examples
let request = ocre::http::Request::post("https://example.com").json(&[1, 2]);
assert_eq!(request.body_bytes(), Some(b"[1,2]".as_slice()));Sourcepub fn form(self, value: &impl Serialize) -> Self
pub fn form(self, value: &impl Serialize) -> Self
Sends value as a form (Content-Type: application/x-www-form-urlencoded).
§Examples
let request = ocre::http::Request::post("https://example.com/token").form(&[("grant_type", "client_credentials")]);
assert_eq!(request.body_bytes(), Some(b"grant_type=client_credentials".as_slice()));Sourcepub fn body(self, bytes: impl Into<Vec<u8>>, content_type: &str) -> Self
pub fn body(self, bytes: impl Into<Vec<u8>>, content_type: &str) -> Self
Sends bytes as the body, with Content-Type: content_type.
§Examples
let request = ocre::http::Request::put("https://example.com/a.txt").body(b"hello".to_vec(), "text/plain");
assert_eq!(request.headers(), [("content-type".to_owned(), "text/plain".to_owned())]);Sourcepub fn timeout(self, timeout: Duration) -> Self
pub fn timeout(self, timeout: Duration) -> Self
Gives up waiting for the answer after timeout (default DEFAULT_TIMEOUT).
§Examples
use std::time::Duration;
let request = ocre::http::Request::get("https://example.com").timeout(Duration::from_secs(5));
assert_eq!(request.timeout_duration(), Duration::from_secs(5));Sourcepub fn follow_redirects(self, follow: bool) -> Self
pub fn follow_redirects(self, follow: bool) -> Self
Whether to follow redirects (default true); with false, a 3xx is the answer.
§Examples
let request = ocre::http::Request::post("https://hooks.example.com").follow_redirects(false);
assert!(!request.follows_redirects());Sourcepub fn headers(&self) -> &[(String, String)]
pub fn headers(&self) -> &[(String, String)]
The headers, lowercase names, in the order they were added.
Sourcepub fn body_bytes(&self) -> Option<&[u8]>
pub fn body_bytes(&self) -> Option<&[u8]>
The body, if any.
Sourcepub fn timeout_duration(&self) -> Duration
pub fn timeout_duration(&self) -> Duration
The timeout.
Sourcepub fn follows_redirects(&self) -> bool
pub fn follows_redirects(&self) -> bool
Whether redirects are followed.
Source§impl Request
impl Request
Sourcepub fn send(self) -> impl Future<Output = Result<Response>> + Send + use<>
pub fn send(self) -> impl Future<Output = Result<Response>> + Send + use<>
Sends the request and returns the answer, whatever its status.
The returned future is Send: await it in a handler, a job or a
scheduled task. Only runs on Workers (natively it panics, like every
binding); request tests reach it through the app.
§Errors
Error::Internal (500) when the body could
not be serialized, the URL or a header is invalid, the service cannot
be reached, or no answer came within the timeout.