rustiron

How do I create a custom Content-Type with Iron?


I want to create the header Content-Type: application/x-protobuf in my Iron web app.

I can see from the Iron docs that it's possible to construct content-types, but there's no SubLevel that corresponds to protobuf.

How can I create this content-type value?

let mut headers = Headers::new();
headers.set(ContentType(Mime(TopLevel::Application, SubLevel::???, vec![])));

Solution

  • but there's no SubLevel that corresponds to protobuf.

    If you read the documentation for SubLevel, you'll see its definition:

    pub enum SubLevel {
        Star,
        Plain,
        // ... snip ...
        Ogg,
        Ext(String),
    }
    

    Thus, you need:

    extern crate iron; // 0.6.0
    
    use iron::{
        headers::ContentType,
        mime::{Mime, SubLevel, TopLevel},
        Headers,
    };
    
    fn main() {
        let mut headers = Headers::new();
        headers.set(ContentType(Mime(
            TopLevel::Application,
            SubLevel::Ext("x-protobuf".into()),
            vec![],
        )));
    }