HttpResponse::Ok() header that can not be transformed to string

Hi,

I work with actix-web. I make a response that send a pdf and I put a report in header. So report is a custom header.
I write some string with some accent in the report that has a json value.

HttpResponse::Ok()
                    .append_header(("REPORT", json))
                    .content_type("application/pdf")
                    .body(pdf)),

But when I get the answer I have some character that can not be transformed in string.

 g\xc3\xa9n\xc3\xa9r\xc3\xa9

Do you know a way to have a good string formed ? Because when it is in the body, there is no problem, just in header...

HTTP headers are ASCII only. I'd encode the report in base64.

3 Likes

Ooooooh !
Thank you. I will try some ways tomorow !

Thanks, I finally use this function to pass from ascii to utf8 and it is ok : std::str::from_utf8(header_content.as_bytes())

If you cannot guarantee that all bytes of the header are valid ASCII, you should not pass those into the header in the first place. A proper encoding scheme that guarantees ASCII, like the suggested base64, should prove more stable when it comes to protocol compliance.

3 Likes

In my case I can guarentee. But it is a very good point ! Thank you !

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.