@@ -18,8 +18,88 @@ use spin_executor::bindings::wasi::io::streams::{self, StreamError};
1818
1919/// A unified request object that can represent both incoming and outgoing requests.
2020///
21- /// This should be used in favor of ` IncomingRequest` and ` OutgoingRequest` when there
21+ /// This should be used in favor of [ IncomingRequest] and [ OutgoingRequest] when there
2222/// is no need for streaming bodies.
23+ ///
24+ /// # Examples
25+ ///
26+ /// Read the method, a header, and the body an incoming HTTP request, without streaming.
27+ ///
28+ /// ```ignore
29+ /// use spin_sdk::http::{Method, Request, Response};
30+ /// use spin_sdk::http_component;
31+ ///
32+ /// #[http_component]
33+ /// fn handle_request(req: Request) -> anyhow::Result<Response> {
34+ /// let method = req.method();
35+ /// let content_type = req.header("content-type");
36+ /// if *method == Method::Post {
37+ /// let body = String::from_utf8_lossy(req.body());
38+ /// }
39+ /// todo!()
40+ /// }
41+ /// ```
42+ ///
43+ /// Send an outgoing GET request (no body) to `example.com`.
44+ ///
45+ /// ```no_run
46+ /// use spin_sdk::http::{Request, Response};
47+ ///
48+ /// # #[tokio::main]
49+ /// # async fn main() -> anyhow::Result<()> {
50+ /// let request = Request::get("example.com");
51+ /// let response: Response = spin_sdk::http::send(request).await?;
52+ /// # Ok(())
53+ /// # }
54+ /// ```
55+ ///
56+ /// Send an outgoing POST request with a non-streaming body to `example.com`.
57+ ///
58+ /// ```no_run
59+ /// use spin_sdk::http::{Request, Response};
60+ ///
61+ /// # #[tokio::main]
62+ /// # async fn main() -> anyhow::Result<()> {
63+ /// let request = Request::post("example.com", "it's a-me, Spin")
64+ /// .header("content-type", "text/plain")
65+ /// .build();
66+ /// let response: Response = spin_sdk::http::send(request).await?;
67+ /// # Ok(())
68+ /// # }
69+ /// ```
70+ ///
71+ /// Build and send an outgoing request without using the helper shortcut.
72+ ///
73+ /// ```no_run
74+ /// use spin_sdk::http::{Method, Request, Response};
75+ ///
76+ /// # #[tokio::main]
77+ /// # async fn main() -> anyhow::Result<()> {
78+ /// let mut request = Request::new(Method::Put, "https://example.com/message/safety");
79+ /// request.set_header("content-type", "text/plain");
80+ /// *request.body_mut() = "beware the crocodile".as_bytes().to_vec();
81+ /// let response: Response = spin_sdk::http::send(request).await?;
82+ /// # Ok(())
83+ /// # }
84+ /// ```
85+ ///
86+ /// Build and send an outgoing request using the fluent builder.
87+ ///
88+ /// ```no_run
89+ /// use spin_sdk::http::{Method, Request, Response};
90+ ///
91+ /// # #[tokio::main]
92+ /// # async fn main() -> anyhow::Result<()> {
93+ /// let request = Request::builder()
94+ /// .uri("https://example.com/message/motivational")
95+ /// .method(Method::Put)
96+ /// .header("content-type", "text/plain")
97+ /// .body("the capybaras of creativity fly higher than the bluebirds of banality")
98+ /// .build();
99+ /// let response: Response = spin_sdk::http::send(request).await?;
100+ /// # Ok(())
101+ /// # }
102+ /// ```
23103pub struct Request {
24104 /// The method of the request
25105 method : Method ,
@@ -613,6 +693,59 @@ impl ResponseOutparam {
613693}
614694
615695/// Send an outgoing request
696+ ///
697+ /// # Examples
698+ ///
699+ /// Get the example.com home page:
700+ ///
701+ /// ```no_run
702+ /// use spin_sdk::http::{Request, Response};
703+ ///
704+ /// # #[tokio::main]
705+ /// # async fn main() -> anyhow::Result<()> {
706+ /// let request = Request::get("example.com").build();
707+ /// let response: Response = spin_sdk::http::send(request).await?;
708+ /// println!("{}", response.body().len());
709+ /// # Ok(())
710+ /// # }
711+ /// ```
712+ ///
713+ /// Use the `http` crate Request type to send a data transfer value:
714+ ///
715+ /// ```no_run
716+ /// use hyperium::Request;
717+ ///
718+ /// #[derive(serde::Serialize)]
719+ /// struct User {
720+ /// name: String,
721+ /// }
722+ ///
723+ /// impl spin_sdk::http::conversions::TryIntoBody for User {
724+ /// type Error = serde_json::Error;
725+ ///
726+ /// fn try_into_body(self) -> Result<Vec<u8>, Self::Error> {
727+ /// serde_json::to_vec(&self)
728+ /// }
729+ /// }
730+ ///
731+ /// # #[tokio::main]
732+ /// # async fn main() -> anyhow::Result<()> {
733+ /// let user = User {
734+ /// name: "Alice".to_owned(),
735+ /// };
736+ ///
737+ /// let request = hyperium::Request::builder()
738+ /// .method("POST")
739+ /// .uri("https://example.com/users")
740+ /// .header("content-type", "application/json")
741+ /// .body(user)?;
742+ ///
743+ /// let response: hyperium::Response<()> = spin_sdk::http::send(request).await?;
744+ ///
745+ /// println!("{}", response.status().is_success());
746+ /// # Ok(())
747+ /// # }
748+ /// ```
616749pub async fn send < I , O > ( request : I ) -> Result < O , SendError >
617750where
618751 I : TryIntoOutgoingRequest ,
0 commit comments