Cannot set headers of an Iron framework Response

Jacob Clark

I am looking to set the headers of a Iron Response with the following code:

extern crate iron;  // 0.3.0
extern crate hyper; // 0.8.1

use iron::prelude::*;
use iron::status;

use hyper::header::{Headers, ContentType};
use hyper::mime::{Mime, TopLevel, SubLevel};

use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

fn main() {
    fn hello_world(_: &mut Request) -> IronResult<Response> {
        let mut headers = Headers::new();
        let string = getFileAsString("./public/index.html");

        headers.set(
            ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![]))
        );

        Ok(Response::with((status::Ok, string, headers)))
    }

    Iron::new(hello_world).http("localhost:3000").unwrap();
    println!("On 3000");
}

fn getFileAsString(fileStr: &str) -> String {
    let path = Path::new(fileStr);
    let display = path.display();
    let mut fileContents = String::new();

    let mut file = match File::open(&path) {
        Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
        Ok(file) => file,
    };

    match file.read_to_string(&mut fileContents) {
        Err(why) => panic!("couldn't read {}: {}", display, Error::description(&why)),
        Ok(_) => fileContents
    }    
}

However I get the error:

error[E0277]: the trait bound `iron::Headers: iron::modifier::Modifier<iron::Response>` is not satisfied
  --> src/main.rs:24:12
   |
24 |         Ok(Response::with((status::Ok, string, headers)))
   |            ^^^^^^^^^^^^^^ the trait `iron::modifier::Modifier<iron::Response>` is not implemented for `iron::Headers`
   |
   = note: required because of the requirements on the impl of `iron::modifier::Modifier<iron::Response>` for `(hyper::status::StatusCode, std::string::String, iron::Headers)`
   = note: required by `iron::Response::with`

Why am I not able to pass headers into this tuple to be modified by the Request builder?

Shepmaster

You can modify the headers on the Response object:

fn hello_world(_: &mut Request) -> IronResult<Response> {
    let string = get_file_as_string("./public/index.html");
    let mut resp = Response::with((status::Ok, string));
    resp.headers.set(ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![])));
    Ok(resp)
}

To figure out the original error, let's check the error message:

error[E0277]: the trait bound `iron::Headers: iron::modifier::Modifier<iron::Response>` is not satisfied
  --> src/main.rs:24:12
   |
24 |         Ok(Response::with((status::Ok, string, headers)))
   |            ^^^^^^^^^^^^^^ the trait `iron::modifier::Modifier<iron::Response>` is not implemented for `iron::Headers`
   |
   = note: required because of the requirements on the impl of `iron::modifier::Modifier<iron::Response>` for `(hyper::status::StatusCode, std::string::String, iron::Headers)`
   = note: required by `iron::Response::with`

The first line tells us the immediate issue: iron::Headers does not implement the trait iron::modifier::Modifier<iron::Response>. If we check the documentation for Headers, we can see under the Trait Implementations section that it indeed does not implement Modifier.

We can then look at the problem from the other end: what does implement Modifier? The docs for Modifier, when built in conjunction with Iron, answer that question. One thing we can see is:

impl<H> Modifier<Response> for Header<H>
where
    H: Header + HeaderFormat,

This leads to an alternate possibility:

use iron::modifiers::Header;

fn hello_world(_: &mut Request) -> IronResult<Response> {
    let string = get_file_as_string("./public/index.html");
    let content_type = Header(ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![])));
    Ok(Response::with((status::Ok, string, content_type)))
}

And if we look at the implementation of Modifier for Header:

fn modify(self, res: &mut Response) {
    res.headers.set(self.0);
}

It just sets the headers as we did above.


FYI, Rust style is snake_case for variables and methods and Error::description(&why) is normally written why.description().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Scalatra Set Response Headers

From Dev

Custom response headers with Strongloop framework

From Dev

Cannot set headers

From Dev

MultipartEntity: Cannot set headers for StringBody

From Dev

How does the Iron framework apply a tuple of modifiers to Response::with?

From Dev

How does the Iron framework apply a tuple of modifiers to Response::with?

From Dev

Cannot read response headers for CORS request with jQuery

From Dev

AngularJS - Cannot read response headers from $http

From Dev

akka-http: How to set response headers

From Dev

How to set Response Headers with Grails CacheHeaders Plugin?

From Dev

Set Express response headers before redirect

From Dev

How do servers set HTTP response headers?

From Dev

set-cookies in response headers is 'half' working

From Dev

Cannot set headers after they are sent to the client - NodeJs

From Dev

Express Cannot set headers after they are sent

From Dev

Async response - can't set headers after they are sent

From Dev

Get the current response headers set in a Tornado request handler

From Dev

Change character set of downloaded file via response headers

From Dev

Set response headers in jasmine-ajax mock responses?

From Dev

iron-ajax response function

From Dev

Polymer Iron-Form response

From Dev

Polymer Iron-Form response

From Dev

How to set up Browser Caching and Expiry Headers on the Phoenix Framework?

From Dev

NodeJS Cannot set headers error when I'm not setting any

From Dev

NodeJS - Cannot set headers after they are sent - Multiple Calls

From Dev

Node.js "Cannot set headers after they are sent" error message

From Dev

Cannot set headers after they are sent to the client - Node/Mongoose/Express

From Dev

Cannot set Headers after they are sent while writing cookies

From Dev

HTTP Response Header is being overwritten. Where all can HTTP Response headers be set in apache?

Related Related

  1. 1

    Scalatra Set Response Headers

  2. 2

    Custom response headers with Strongloop framework

  3. 3

    Cannot set headers

  4. 4

    MultipartEntity: Cannot set headers for StringBody

  5. 5

    How does the Iron framework apply a tuple of modifiers to Response::with?

  6. 6

    How does the Iron framework apply a tuple of modifiers to Response::with?

  7. 7

    Cannot read response headers for CORS request with jQuery

  8. 8

    AngularJS - Cannot read response headers from $http

  9. 9

    akka-http: How to set response headers

  10. 10

    How to set Response Headers with Grails CacheHeaders Plugin?

  11. 11

    Set Express response headers before redirect

  12. 12

    How do servers set HTTP response headers?

  13. 13

    set-cookies in response headers is 'half' working

  14. 14

    Cannot set headers after they are sent to the client - NodeJs

  15. 15

    Express Cannot set headers after they are sent

  16. 16

    Async response - can't set headers after they are sent

  17. 17

    Get the current response headers set in a Tornado request handler

  18. 18

    Change character set of downloaded file via response headers

  19. 19

    Set response headers in jasmine-ajax mock responses?

  20. 20

    iron-ajax response function

  21. 21

    Polymer Iron-Form response

  22. 22

    Polymer Iron-Form response

  23. 23

    How to set up Browser Caching and Expiry Headers on the Phoenix Framework?

  24. 24

    NodeJS Cannot set headers error when I'm not setting any

  25. 25

    NodeJS - Cannot set headers after they are sent - Multiple Calls

  26. 26

    Node.js "Cannot set headers after they are sent" error message

  27. 27

    Cannot set headers after they are sent to the client - Node/Mongoose/Express

  28. 28

    Cannot set Headers after they are sent while writing cookies

  29. 29

    HTTP Response Header is being overwritten. Where all can HTTP Response headers be set in apache?

HotTag

Archive