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

pub struct HttpResponse { /* fields omitted */ }

Represents a HTTP output binding.

Responses can be 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!(
    response.body().as_str().unwrap(),
    "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!(
    response.body().as_str().unwrap(),
    "{\"hello\":\"world!\"}"
);

Creating a response from a sequence of bytes:

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

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

assert_eq!(response.status(), Status::Ok);
assert_eq!(
    response
        .headers()
        .get("Content-Type")
        .unwrap(),
    "application/octet-stream"
);
assert_eq!(
    response.body().as_bytes(),
    [1, 2, 3]
);

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!(
    response.body().as_str().unwrap(),
    body
);

Methods

impl HttpResponse[src]

pub fn build() -> ResponseBuilder[src]

Creates a new ResponseBuilder for building a response.

Examples

use azure_functions::bindings::HttpResponse;
use azure_functions::http::Status;

let response: HttpResponse = HttpResponse::build().status(Status::NotFound).into();
assert_eq!(response.status(), Status::NotFound);

pub fn status(&self) -> Status[src]

Gets the status code for the response.

Examples

use azure_functions::bindings::HttpResponse;
use azure_functions::http::Status;

let response: HttpResponse = HttpResponse::build().status(Status::BadRequest).into();
assert_eq!(response.status(), Status::BadRequest);

pub fn body(&self) -> Body[src]

Gets the body of the response.

Examples

use azure_functions::bindings::HttpResponse;

let response: HttpResponse = HttpResponse::build().body("example").into();
assert_eq!(response.body().as_str().unwrap(), "example");

pub fn headers(&self) -> &HashMap<String, String>[src]

Gets the headers of the response.

Examples

use azure_functions::bindings::HttpResponse;

let response: HttpResponse = HttpResponse::build().header("Content-Type", "text/plain").into();
assert_eq!(response.headers().get("Content-Type").unwrap(), "text/plain");

Trait Implementations

impl Default for HttpResponse[src]

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

impl From<ResponseBuilder> for HttpResponse[src]

impl Into<TypedData> for HttpResponse[src]

impl Debug for HttpResponse[src]

Auto Trait Implementations

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Erased for T