RequiredParam does not return output in format specificed by Render middleware

57 views
Skip to first unread message

sent-hil

unread,
Aug 20, 2012, 5:13:43 AM8/20/12
to golia...@googlegroups.com
class UsersCreate < Goliath::API
  use Goliath::Rack::Render, 'json'
  use Goliath::Rack::Params
  use Goliath::Rack::Validation::RequiredParam, {:key          => 'token',
                                                                              :message => 'is required'}
end

If no "token" param, Goliath returns "[:error, \"token is required\"]". Since I've included the Render middleware, shouldn't the output be in json?

Regards.

Ilya Grigorik

unread,
Aug 20, 2012, 1:36:34 PM8/20/12
to golia...@googlegroups.com
Yes, but it also depends on what you're using to send the request.. If your client is specifically asking for text/plain, then that's what you'll get. 

ig

sent-hil

unread,
Aug 20, 2012, 1:54:02 PM8/20/12
to golia...@googlegroups.com
But if its blank I should get a json response right? I'm not seeing that.

Eric Marden

unread,
Aug 21, 2012, 10:43:59 PM8/21/12
to golia...@googlegroups.com
Goliath::Rack::Render only sets the Content-Type header (and sets the Vary header to the value of the Accept header in the request).

Use that with Goliath::Rack::Formatters::JSON to get closer to the results you want.

Keep in mind that these are mechanisms of Content Negotiation (http://en.wikipedia.org/wiki/Content_negotiation) and can depend on the Accept header being sent asking for `application/json` data to be returned.


--
Eric Marden
http://ericmarden.com


On Monday, August 20, 2012 at 12:54 PM, sent-hil wrote:

> But if its blank I should get a json response right? I'm not seeing that.
>

sent-hil

unread,
Aug 21, 2012, 11:38:05 PM8/21/12
to golia...@googlegroups.com
class UsersCreate < Goliath::API
  use Goliath::Rack::Formatters::JSON

  use Goliath::Rack::Render, 'json'
  use Goliath::Rack::Params
  use Goliath::Rack::Validation::RequiredParam, {:key          => 'token',
                                                                              :message => 'is required'}
  def response(env)
    [200, {'Content-Type' => 'application/json'}, {response: 'user created'}]
  end
end

With the above code, if I do `curl -H "Accept:application/json" "http://localhost:8000"` shouldn't I be seeing {'error' => 'token is required'}? Instead I still see [:error, "token is required"].

I included JSON formatter and specified 'Accept' header, not sure what's going on here.

Regards.

Ilya Grigorik

unread,
Aug 25, 2012, 1:06:49 AM8/25/12
to golia...@googlegroups.com
It looks like you're inserting two JSON middlewares in the chain.. which leads to double encoding:

Interestingly enough, we did have a guard for duplicate middlewares in pre 1.0 release, but dropped that as part of refactoring.. May be worth taking another look.

ig

sent-hil

unread,
Aug 25, 2012, 4:25:42 PM8/25/12
to golia...@googlegroups.com
I originally had just the Render middleware with 'json' as arg and I got the text response when calling with no "Accept" header.

I tried it again and it seems to work. Not sure what has changed in the meantime.

Oh well, thanks for your time.
Reply all
Reply to author
Forward
0 new messages