Ruby-on-rails 在 Rails 控制台中为放置/发布请求提供参数哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8474689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Provide params hash for put / post requests in rails console
提问by jibiel
I find it more convenient to check response for some requests from within console
我发现在控制台内检查某些请求的响应更方便
>> app.put '/users/2/'
=> 500
But wasn't able to find a way to specify request parameters. How I have to do that?
但无法找到指定请求参数的方法。我该怎么做?
回答by Nakul
If you want to put or post to a URL there are also methods for that. You can copy/paste the parameters exactly as they are displayed in your Rails production log:
如果你想放置或发布到一个 URL,也有一些方法。您可以完全按照 Rails 生产日志中显示的参数复制/粘贴参数:
app.post('/foo', {"this" => "that", "items" => ["bar", "baz"]})
app.put('/foo', {"this" => "that", "items" => ["bar", "baz"]})
If you want to sent a custom header, you can add an optional third parameter:
如果要发送自定义标头,可以添加可选的第三个参数:
app.post('/foo', {:this => "that", :items => ["bar", "baz"]}, {"X-Do-Something" => "yes"})
Any of the get/post/put/delete methods will display their full log output on the console for you to examine. If you want to get information such as the response body returned, HTTP status or response headers these are easy too:
任何 get/post/put/delete 方法都会在控制台上显示它们的完整日志输出供您检查。如果您想获取诸如返回的响应正文、HTTP 状态或响应标头等信息,这些也很容易:
app.response.body
app.response.status
app.response.headers.inspect
Source: http://andyjeffries.co.uk/articles/debug-level-logging-for-a-single-rails-production-request
资料来源:http: //andyjeffries.co.uk/articles/debug-level-logging-for-a-single-rails-production-request
回答by Jayanti Hari
The above has changed to
以上已改为
app.post '/foo', params: {"this" => "that", "items" => ["bar", "baz"]}
Also for forms I had to give an authenticity_token as well. So in my example the full command was
同样对于表单,我也必须提供一个真实性_令牌。所以在我的例子中,完整的命令是
app.post '/login', params: {email: '[email protected]', password: 'abcd', authenticity_token: 'my_authenticity_token_generated_for_this_view' }

