pub async fn send<I, O>(request: I) -> Result<O, SendError>
Expand description
Send an outgoing request
ยงExamples
Get the example.com home page:
use spin_sdk::http::{Request, Response};
let request = Request::get("example.com").build();
let response: Response = spin_sdk::http::send(request).await?;
println!("{}", response.body().len());
Use the http
crate Request type to send a data transfer value:
use hyperium::Request;
#[derive(serde::Serialize)]
struct User {
name: String,
}
impl spin_sdk::http::conversions::TryIntoBody for User {
type Error = serde_json::Error;
fn try_into_body(self) -> Result<Vec<u8>, Self::Error> {
serde_json::to_vec(&self)
}
}
let user = User {
name: "Alice".to_owned(),
};
let request = hyperium::Request::builder()
.method("POST")
.uri("https://example.com/users")
.header("content-type", "application/json")
.body(user)?;
let response: hyperium::Response<()> = spin_sdk::http::send(request).await?;
println!("{}", response.status().is_success());