add charset to content-type header

71 views
Skip to first unread message

René Maya

unread,
Sep 12, 2017, 9:34:31 PM9/12/17
to Rodauth

Hi, I'm trying to setup Rodauth for a Roda project that checks all responses, whether HTML or JSON, include utf-8 charset.
I read the based, login, and logout sections of the documentation and I can't seem to find any setting related to headers.
I tried something similar to overriding the route behavior http://rodauth.jeremyevans.net/rdoc/files/README_rdoc.html#label-Overriding+Route-Level+Behavior
but I couldn't get rodauth to handle POSTs.
Is there a simple way to edit Rodauth headers?

Thanks,

Rene

Jeremy Evans

unread,
Sep 12, 2017, 10:14:33 PM9/12/17
to Rodauth
Setting the default headers for HTML would be a Roda setting, not a Rodauth setting. In your Roda configuration:

  plugin :default_headers, 'Content-Type'=>'text/html; charset=utf-8'

For setting the content type header for JSON responses in Rodauth, in your Rodauth configuration:

  json_response_content_type 'application/json; charset=utf-8'

Thanks,
Jeremy

René Maya

unread,
Sep 13, 2017, 12:12:05 PM9/13/17
to Rodauth
Thanks that worked! I was a bit surprised since I already had it in the type routing plugin, is that expected?

Cheers,
Rene

Jeremy Evans

unread,
Sep 13, 2017, 1:28:40 PM9/13/17
to Rodauth
On Wednesday, September 13, 2017 at 9:12:05 AM UTC-7, René Maya wrote:
Thanks that worked! I was a bit surprised since I already had it in the type routing plugin, is that expected?

I'm not sure.  If you post a minimal self contained example and explain what you were expecting, I can review it.

Thanks,
Jeremy 

René Maya

unread,
Sep 29, 2017, 8:53:20 PM9/29/17
to Rodauth
Hi Jeremy,

I put this gist together. I was wondering about what happens in lines 81-84. I was expecting type_routing plugin to take care of the Content-Type headers over default_headers plugin and Rodauth for DRY reasons. Like you explained before, that is not the case when using Rodauth JSON responses (lines 87-90) we need to setup line 27.

As I was checking the stuff above I noticed that type_routing only uses the headers defined in the options hash inside the `r.json {}` and `r.html {}` blocks (lines 76-79). Although the block that holds them matches the default_type option it doesn't include the charset definition. Is that because the default_type is expected to be set in the default_headers plugin if we don't use the r.type blocks rather than in the `types` option?

Thanks,

Rene

Jeremy Evans

unread,
Sep 30, 2017, 12:40:51 AM9/30/17
to Rodauth
On Friday, September 29, 2017 at 5:53:20 PM UTC-7, René Maya wrote:
Hi Jeremy,

I put this gist together. I was wondering about what happens in lines 81-84. I was expecting type_routing plugin to take care of the Content-Type headers over default_headers plugin and Rodauth for DRY reasons. Like you explained before, that is not the case when using Rodauth JSON responses (lines 87-90) we need to setup line 27.

The Roda type_routing plugin only sets the Content-Type header if you call the appropriate type_routing method (e.g. r.html).  Rodauth doesn't depend on the type_routing plugin, and therefore does not set the Content-Type used by the type_routing plugin.

Not that I think it affects the output in this case, but looking at the code, I'm guessing 81-90 aren't calling the code you think you are calling.  You probably want to swap lines 52 and 53.
 
As I was checking the stuff above I noticed that type_routing only uses the headers defined in the options hash inside the `r.json {}` and `r.html {}` blocks (lines 76-79). Although the block that holds them matches the default_type option it doesn't include the charset definition. Is that because the default_type is expected to be set in the default_headers plugin if we don't use the r.type blocks rather than in the `types` option?

For line 76, you are calling the root path, but you aren't using an a type routing method in the html case, and as mentioned above, type_routing only sets the content type when you are calling a type_routing method.

I've just added a documentation commit that better explains type_routing's behavior: https://github.com/jeremyevans/roda/commit/461d38a5348da3deaec4439033cea75569abf965

Thanks,
Jeremy

René Maya

unread,
Sep 30, 2017, 6:04:13 AM9/30/17
to Rodauth
Awesome, thanks for the explanation and new details in the documentation!
Reply all
Reply to author
Forward
0 new messages