0

How would you convert the Grape::Request object to a string in order to store it's contents in the database? I tried

request.to_s and only get #<Grape::Request:0x007f7fd34605c8>

I also tried

request.to_json and get this IOError Exception: not opened for reading
3
  • What are the contents of request? Try inspecting that variable or calling it in a debugger like pry or byebug, and posting the results here. Commented Jul 21, 2015 at 23:58
  • Any part of the request in particular? Commented Jul 22, 2015 at 1:37
  • I want everything from the request. There are many things in there such as headers, Params, host, etc. I don't want to have to go through the 50 or so objects in the grape request object. I just want to take all the objects from the grape request object and convert it to a string so I can store it in the database. Commented Jul 22, 2015 at 3:12

1 Answer 1

1

Perhaps the Grape::Endpoint#request method is what you're looking for, although there are others like env that may have more or less of what you need.

I tried it by putting it in the Alongside Sinatra (or other frameworks) example given in the docs:

require 'grape'
require 'sinatra/base'

class API < Grape::API
  get :hello do
    {req: request}
  end
end
class Web < Sinatra::Base
  get '/' do
    "Hello world."
  end
end

run Rack::Cascade.new [API, Web]

and on calling it:

$ [2015-07-22 08:21:17] INFO  WEBrick 1.3.1
[2015-07-22 08:21:17] INFO  ruby 2.2.2 (2015-04-13) [x86_64-darwin13]
[2015-07-22 08:21:17] INFO  WEBrick::HTTPServer#start: pid=30420 port=9292
  curl http://localhost:9292/hello
127.0.0.1 - - [22/Jul/2015:08:21:19 +0100] "GET /hello HTTP/1.1" 200 3665 0.0389
{:req=>#<Grape::Request:0x007f9971425d08 @env={"GATEWAY_INTERFACE"=>"CGI/1.1", "PATH_INFO"=>"/hello", "QUERY_STRING"=>"", "REMOTE_ADDR"=>"127.0.0.1", "REMOTE_HOST"=>"localhost", "REQUEST_METHOD"=>"GET", "REQUEST_URI"=>"http://localhost:9292/hello", "SCRIPT_NAME"=>"", "SERVER_NAME"=>"localhost", "SERVER_PORT"=>"9292", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13)", "HTTP_USER_AGENT"=>"curl/7.30.0", "HTTP_HOST"=>"localhost:9292", "HTTP_ACCEPT"=>"*/*", "rack.version"=>[1, 3], "rack.input"=>#<Rack::Lint::InputWrapper:0x007f99711d6458 @input=#<StringIO:0x007f99711e52f0>>, "rack.errors"=>#<Rack::Lint::ErrorWrapper:0x007f99711d6430 @error=#<IO:<STDERR>>>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "rack.url_scheme"=>"http", "rack.hijack?"=>true, "rack.hijack"=>#<Proc:0x007f99711dc3d0@/Users/iainb/Projects/Test/31551252/vendor.noindex/ruby/2.2.0/gems/rack-1.6.4/lib/rack/lint.rb:525>, "rack.hijack_io"=>nil, "HTTP_VERSION"=>"HTTP/1.1", "REQUEST_PATH"=>"/hello", "sinatra.commonlogger"=>true, "rack.tempfiles"=>[], "rack.routing_args"=>{:route_info=>#<Grape::Route:0x007f997106fcb8 @options={:params=>{}, :prefix=>nil, :version=>nil, :namespace=>"/", :method=>"GET", :path=>"/hello(.:format)", :compiled=>/\A\/hello(?:\.(?<format>[^\/.?]+))?\Z/, :settings=>{}}>}, "api.endpoint"=>#<Grape::Endpoint:0x007f9971497750 @inheritable_setting=#<Grape::Util::InheritableSetting:0x007f9971157040 @route={:saved_declared_params=>[], :saved_validations=>[]}, @api_class={}, @namespace=#<Grape::Util::InheritableValues:0x007f9971156820 @inherited_values={}, @new_values={}>, @namespace_inheritable=#<Grape::Util::InheritableValues:0x007f9971156780 @inherited_values=#<Grape::Util::InheritableValues:0x007f99711c5860 @inherited_values={}, @new_values={}>, @new_values={:default_error_status=>500}>, @namespace_stackable=#<Grape::Util::StackableValues:0x007f9971156618 @inherited_values=#<Grape::Util::StackableValues:0x007f99711c57c0 @inherited_values={}, @new_values={}, @froozen_values={}>, @new_values={}, @froozen_values={}>, @point_in_time_copies=[], @parent=#<Grape::Util::InheritableSetting:0x007f99711c5950 @route={}, @api_class={}, @namespace=#<Grape::Util::InheritableValues:0x007f99711c58d8 @inherited_values={}, @new_values={}>, @namespace_inheritable=#<Grape::Util::InheritableValues:0x007f99711c5860 @inherited_values={}, @new_values={}>, @namespace_stackable=#<Grape::Util::StackableValues:0x007f99711c57c0 @inherited_values={}, @new_values={}, @froozen_values={}>, @point_in_time_copies=[], @parent=nil>>, @options={:method=>["GET"], :path=>[:hello], :for=>API, :route_options=>{:params=>{}}}, @source=#<Proc:0x007f99711c5450@/Users/iainb/Projects/Test/31551252/config.ru:5>, @block=#<Proc:0x007f9971143cc0@/Users/iainb/Projects/Test/31551252/vendor.noindex/ruby/2.2.0/gems/grape-0.12.0/lib/grape/endpoint.rb:47>, @namespace="/", @routes=[#<Grape::Route:0x007f997106fcb8 @options={:params=>{}, :prefix=>nil, :version=>nil, :namespace=>"/", :method=>"GET", :path=>"/hello(.:format)", :compiled=>/\A\/hello(?:\.(?<format>[^\/.?]+))?\Z/, :settings=>{}}>], @env={...}, @header={}, @request=#<Grape::Request:0x007f9971425d08 ...>, @params=#<Hashie::Mash>, @headers={"User-Agent"=>"curl/7.30.0", "Host"=>"localhost:9292", "Accept"=>"*/*", "Version"=>"HTTP/1.1"}, @cookies=#<Grape::Cookies:0x007f99713be090 @cookies={}, @send_cookies={}>>, "api.format"=>:txt, "rack.request.query_string"=>"", "rack.request.query_hash"=>{}, "rack.request.cookie_hash"=>{}}, @params=#<Hashie::Mash>, @headers={"User-Agent"=>"curl/7.30.0", "Host"=>"localhost:9292", "Accept"=>"*/*", "Version"=>"HTTP/1.1"}>}%
Sign up to request clarification or add additional context in comments.

2 Comments

The reason I need to convert the request object to a string is because I get a database error when trying to save the record. TypeError Exception: can't cast Grape::Request to string api_request = ApiRequest.new api_request.request_object = request api_request.created = Time.now api_request.save
Then you probably want request.env. I'd suggest installing something like pry-byebug and putting a binding.pry in your resource and then you can have a look at the request object easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.