Struct azure_functions::bindings::HttpResponse[][src]

pub struct HttpResponse { /* fields omitted */ }

Represents a HTTP output binding.

Usage

Responses can be easily created for any type that implements Into<Body>.

Examples

Creating a response from a string:

use azure_functions::bindings::HttpResponse;
use azure_functions::http::{Body, Status};

let response: HttpResponse = "hello world".into();

assert_eq!(response.status(), Status::Ok);
assert_eq!(
    response
        .headers()
        .get("Content-Type")
        .unwrap(),
    "text/plain");
assert_eq!(
    match response.body() {
        Body::String(s) => s,
        _ => panic!("unexpected body.")
    },
    "hello world"
);

Creating a response from a JSON value (see the json! macro from the serde_json crate):

use azure_functions::bindings::HttpResponse;
use azure_functions::http::{Body, Status};

let response: HttpResponse = json!({ "hello": "world!" }).into();

assert_eq!(response.status(), Status::Ok);
assert_eq!(
    response
        .headers()
        .get("Content-Type")
        .unwrap(),
    "application/json"
);
assert_eq!(
    match response.body() {
        Body::Json(s) => s,
        _ => panic!("unexpected body.")
    },
    "{\"hello\":\"world!\"}"
);

Creating a response from a sequence of bytes:

use azure_functions::bindings::HttpResponse;
use azure_functions::http::{Body, Status};

let response: HttpResponse = [1u8, 2u8, 3u8][..].into();

assert_eq!(response.status(), Status::Ok);
assert_eq!(
    response
        .headers()
        .get("Content-Type")
        .unwrap(),
    "application/octet-stream"
);
assert_eq!(
    &match response.body() {
        Body::Bytes(bytes) => bytes,
        _ => panic!("unexpected body.")
    }[..],
    [1u8, 2u8, 3u8]
);

Building a custom response:

use azure_functions::bindings::HttpResponse;
use azure_functions::http::{Body, Status};

let url = "http://example.com";
let body = format!("The requested resource has moved to: {}", url);

let response: HttpResponse = HttpResponse::build()
    .status(Status::MovedPermanently)
    .header("Location", url)
    .body(body.as_str())
    .into();

assert_eq!(response.status(), Status::MovedPermanently);
assert_eq!(
    response
        .headers()
        .get("Location")
        .unwrap(),
    url
);
assert_eq!(
    match response.body() {
        Body::String(s) => s,
        _ => panic!("unexpected body.")
    },
    body
);

Methods

impl HttpResponse
[src]

Creates a new ResponseBuilder for building a response.

Gets the status code for the response.

Gets the body of the response.

Gets the headers of the response.

Trait Implementations

impl Debug for HttpResponse
[src]

Formats the value using the given formatter. Read more

impl<'a, T> From<T> for HttpResponse where
    T: Into<Body<'a>>, 
[src]

Performs the conversion.

impl<'a> From<&'a mut ResponseBuilder> for HttpResponse
[src]

Performs the conversion.

Auto Trait Implementations