Re: [ruby-roda] Use JsonApi content-type ('application/vnd.api+json') for ALL responses

120 views
Skip to first unread message

Jeremy Evans

unread,
Mar 12, 2021, 1:49:24 PM3/12/21
to ruby...@googlegroups.com
On Fri, Mar 12, 2021 at 10:40 AM 'fran.em...@googlemail.com' via Roda <ruby...@googlegroups.com> wrote:
Hi Jeremy,

I hope this is an easy question but I have read all the docs for all the plugins we are using and I can't see what I've missed.

We have roda based app which is an API almost exclusively receiving and responding in the JsonApi format which uses the special content type 'application/vnd.api+json' . 

We use the following plugins setup in our router class as follows:

```ruby
class Api < Roda
  JSON_CLASSES = [Array, Hash, Roar::Decorator, Representable::Decorator].freeze
  DEFAULT_CONTENT_TYPE = 'application/vnd.api+json'

  plugin :halt
  plugin :multi_route
  plugin :json, classes: JSON_CLASSES, content_type: DEFAULT_CONTENT_TYPE
  plugin :json_parser
  plugin :all_verbs
  plugin :request_headers
  plugin :default_headers, 'Content-Type' => DEFAULT_CONTENT_TYPE

  plugin :rodauth, json: :only do
    enable :jwt
    json_response_error_status 401
    json_response_content_type DEFAULT_CONTENT_TYPE
  end
end
```

Unfortunately for any requests which make use of the json plugin (i.e. parse the routes return value `to_json`), the content-type header is still set to `application/json`. 

Any ideas, I'm wondering if either I've missed a configuration option somewhere or if there is a conflict in setting the content-type heading between the various plugins we are using.

Any help or advice you can give on this would be most appreciated.

Fran,

Can you please put together a minimal self contained reproducible example?  I tried and couldn't reproduce:

class Api < Roda
  DEFAULT_CONTENT_TYPE = 'application/vnd.api+json'

  plugin :json, content_type: DEFAULT_CONTENT_TYPE
  plugin :default_headers, 'Content-Type' => DEFAULT_CONTENT_TYPE

  route do |r|
    r.get "json" do
      [1]
    end

    "true"
  end
end

p Api.call({'REQUEST_METHOD'=>"GET", "PATH_INFO"=>"/json"})
p Api.call({})


Output:

[200, {"Content-Type"=>"application/vnd.api+json", "Content-Length"=>"3"}, ["[1]"]]
[200, {"Content-Type"=>"application/vnd.api+json", "Content-Length"=>"4"}, ["true"]]


This doesn't test everything you are using in your example (no Rodauth), but I'm not sure if your issue specific to Rodauth or not.

Thanks,
Jeremy
Reply all
Reply to author
Forward
0 new messages