Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ordering of set_header and body_string matters #477

Closed
prabirshrestha opened this issue Apr 26, 2020 · 4 comments
Closed

ordering of set_header and body_string matters #477

prabirshrestha opened this issue Apr 26, 2020 · 4 comments

Comments

@prabirshrestha
Copy link

This doesn't set the response header has text/html

 let res = Response::new(StatusCode::Ok)
        .set_header("content-type".parse().unwrap(), "text/html;charset=utf-8")
        .body_string("Hello<br/>".to_owned());

This one does set it correctly.

 let res = Response::new(StatusCode::Ok)
        .body_string("Hello<br/>".to_owned())
        .set_header("content-type".parse().unwrap(), "text/html;charset=utf-8");

tide = "0.8.0"

@Fishrock123
Copy link
Member

The docs for res.body_string explicitly say

The encoding is set to text/plain; charset=utf-8.

I do think it would probably be more developer friendly for this function to check if the content-type header has been set yet or not, though.

Also, if I may, recommend setting the content type through res.set_mime, like tide does internally. See https://docs.rs/mime/0.3.16/mime/index.html#constants.

res.set_mime(mime::TEXT_PLAIN_UTF_8)

@Fishrock123
Copy link
Member

Also, it's not immediately clear but res.set_body(string) should also work. res.body_string() just does that internally.

@prabirshrestha
Copy link
Author

Seems like set_body need to return self to make it easy to chain else need to create temp mutable vars.

@Fishrock123
Copy link
Member

See also: #562

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants