pub struct Request { /* private fields */ }
Expand description
A unified request object that can represent both incoming and outgoing requests.
This should be used in favor of IncomingRequest and OutgoingRequest when there is no need for streaming bodies.
§Examples
Read the method, a header, and the body an incoming HTTP request, without streaming:
fn handle_request(req: Request) -> anyhow::Result<Response> {
let method = req.method();
let content_type = req.header("content-type");
if *method == Method::Post {
let body = String::from_utf8_lossy(req.body());
}
todo!()
}
Send an outgoing GET request (no body) to example.com
:
use spin_sdk::http::{Request, Response};
let request = Request::get("https://example.com");
let response: Response = spin_sdk::http::send(request).await?;
Send an outgoing POST request with a non-streaming body to example.com
.
use spin_sdk::http::{Request, Response};
let request = Request::post("https://example.com", "it's a-me, Spin")
.header("content-type", "text/plain")
.build();
let response: Response = spin_sdk::http::send(request).await?;
Build and send an outgoing request without using the helper shortcut.
use spin_sdk::http::{Method, Request, Response};
let mut request = Request::new(Method::Put, "https://example.com/message/safety");
request.set_header("content-type", "text/plain");
*request.body_mut() = "beware the crocodile".as_bytes().to_vec();
let response: Response = spin_sdk::http::send(request).await?;
Build and send an outgoing request using the fluent builder.
use spin_sdk::http::{Method, Request, Response};
let request = Request::builder()
.uri("https://example.com/message/motivational")
.method(Method::Put)
.header("content-type", "text/plain")
.body("the capybaras of creativity fly higher than the bluebirds of banality")
.build();
let response: Response = spin_sdk::http::send(request).await?;
Implementations§
Source§impl Request
impl Request
Sourcepub fn new(method: Method, uri: impl Into<String>) -> Self
pub fn new(method: Method, uri: impl Into<String>) -> Self
Creates a new request from a method and uri
Sourcepub fn builder() -> RequestBuilder
pub fn builder() -> RequestBuilder
Creates a RequestBuilder
Sourcepub fn get(uri: impl Into<String>) -> RequestBuilder
pub fn get(uri: impl Into<String>) -> RequestBuilder
Creates a RequestBuilder
to GET the given uri
Sourcepub fn post(uri: impl Into<String>, body: impl IntoBody) -> RequestBuilder
pub fn post(uri: impl Into<String>, body: impl IntoBody) -> RequestBuilder
Creates a RequestBuilder
to POST the given body
to uri
Sourcepub fn put(uri: impl Into<String>, body: impl IntoBody) -> RequestBuilder
pub fn put(uri: impl Into<String>, body: impl IntoBody) -> RequestBuilder
Creates a RequestBuilder
to PUT the given body
to uri
Sourcepub fn patch(uri: impl Into<String>, body: impl IntoBody) -> RequestBuilder
pub fn patch(uri: impl Into<String>, body: impl IntoBody) -> RequestBuilder
Creates a RequestBuilder
to PATCH the resource specified by uri
Sourcepub fn delete(uri: impl Into<String>) -> RequestBuilder
pub fn delete(uri: impl Into<String>) -> RequestBuilder
Creates a RequestBuilder
to DELETE the resource specified by uri
Sourcepub fn headers(&self) -> impl Iterator<Item = (&str, &HeaderValue)>
pub fn headers(&self) -> impl Iterator<Item = (&str, &HeaderValue)>
The request headers
Sourcepub fn header(&self, name: &str) -> Option<&HeaderValue>
pub fn header(&self, name: &str) -> Option<&HeaderValue>
Return a header value
Will return None
if the header does not exist.
Sourcepub fn path_and_query(&self) -> Option<&str>
pub fn path_and_query(&self) -> Option<&str>
The request path and query combined
Trait Implementations§
Source§impl TryFromIncomingRequest for Request
impl TryFromIncomingRequest for Request
Source§type Error = IncomingRequestError
type Error = IncomingRequestError
Source§fn try_from_incoming_request<'async_trait>(
request: IncomingRequest,
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
fn try_from_incoming_request<'async_trait>(
request: IncomingRequest,
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
IncomingRequest
into the implementing type