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]
impl HttpResponse
pub fn build() -> ResponseBuilder
[src]
pub fn build() -> ResponseBuilder
Creates a new ResponseBuilder for building a response.
pub fn status(&self) -> Status
[src]
pub fn status(&self) -> Status
Gets the status code for the response.
pub fn body(&self) -> Body
[src]
pub fn body(&self) -> Body
Gets the body of the response.
pub fn headers(&self) -> &HashMap<String, String>
[src]
pub fn headers(&self) -> &HashMap<String, String>
Gets the headers of the response.
Trait Implementations
impl Debug for HttpResponse
[src]
impl Debug for HttpResponse
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<'a, T> From<T> for HttpResponse where
T: Into<Body<'a>>,
[src]
impl<'a, T> From<T> for HttpResponse where
T: Into<Body<'a>>,
impl<'a> From<&'a mut ResponseBuilder> for HttpResponse
[src]
impl<'a> From<&'a mut ResponseBuilder> for HttpResponse
fn from(builder: &'a mut ResponseBuilder) -> Self
[src]
fn from(builder: &'a mut ResponseBuilder) -> Self
Performs the conversion.
Auto Trait Implementations
impl Send for HttpResponse
impl Send for HttpResponse
impl Sync for HttpResponse
impl Sync for HttpResponse