如何将Grape :: Request对象转换为字符串,以便将其内容存储在数据库中?我试过了
request.to_s and only get #<Grape::Request:0x007f7fd34605c8>
我也试过
request.to_json and get this IOError Exception: not opened for reading
答案 0 :(得分:1)
也许Grape::Endpoint#request
方法正是您正在寻找的方法,尽管还有其他类似env
的方法可能会有更多或更少的需求。
我尝试将其放在文档中提供的Alongside Sinatra (or other frameworks)示例中:
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]
并且在调用它时:
$ [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"}>}%