Response in spin_sdk::http - Rust (original) (raw)

pub struct Response { /* private fields */ }

Expand description

A unified response object that can represent both outgoing and incoming responses.

This should be used in favor of OutgoingResponse and IncomingResponse when there is no need for streaming bodies.

§Examples

Send a response to an incoming HTTP request:

use spin_sdk::http::{Request, Response};

fn handle_request(req: Request) -> anyhow::Result<Response> {
    Ok(Response::builder()
        .status(200)
        .header("content-type", "text/plain")
        .body("Hello, world")
        .build())
}

Parse a response from an outgoing HTTP request:

#[derive(serde::Deserialize)]
struct User {
    name: String,
}

let request = Request::get("https://example.com");
let response: Response = spin_sdk::http::send(request).await?;
if *response.status() == 200 {
    let body = response.body();
    let user: User = serde_json::from_slice(body)?;
}

Source§

Source

Create a new response from a status and body

Source

The response status

The request headers

Return a header value

Will return None if the header does not exist.

Set a response header

Source

The response body

Source

The response body

Source

Consume this type and return its body

Source

Converts this response into a ResponseBuilder. This can be used to update a response before passing it on.

Source

§

§

§

§

§

§