Skip to content

Responses

Each HTTP request served by Rwf is expected to return a response. If your app is using a REST API, responses are typically JSON. If you prefer HTML over the wire or plain old websites, the responses will contain HTML or text.

Creating responses

To create a response, you can just instantiate the Response struct and populate the body with the right content. The most popular response types have their own instantiation methods in Rwf:

let response = Response::new()
  .html("<h1>Big letters!</h1>");
let json = serde_json::json!({
  "id": 5,
  "email": "test@example.com"
});

let response = Response::new().json(json)?;
let response = Response::new()
  .text("One apple a day keeps the doctor away!");

Using one of those methods will automatically set the right Content-Type and Content-Length headers.

Raw data

If your endpoint is sending binary data or some data type we don't have a method for, you can always set the body and content type manually:

let mystery_bytes: Vec<u8> = vec![1, 1, 2, 3, 5, 8, 13];

let response = Response::new()
  .header("Content-Type", "application/octet-stream")
  .body(mystery_bytes);

The Content-Length header is always set automatically, but if you absolutely need to, you can set it manually using the header method.

Headers

Setting custom headers can be done with the header method:

let response = Response::new()
  .header("X-My-Header", "My value")
  .header("Cache", "no-store");

Learn more