有没有办法直接通过 Ruby on Rails 控制台调用 Grape API,并在调用过程中传递适当的参数

问题描述 投票:0回答:1

我想通过 Rails 控制台直接调用 Grape API 作为 Rails 应用程序的一部分。我们如何直接从 Rails 控制台调用 Grape API,并在调用过程中传递适当的参数?

我目前使用的是 Rails 7,并且正在使用 Grape 2.0.0

ruby-on-rails ruby rails-console ruby-grape
1个回答
0
投票

是的。

Rails 控制台中的“会话”实际上是一个 ActionDispatch::IntegrationTest 实例。这有多疯狂?

您可以通过在特殊方法上调用它来发送任何 HTTP 方法

app
:

irb(main):004:0> app.get "/", params: { foo: :bar }
Started GET "/?foo=bar" for 127.0.0.1 at 2024-04-17 18:40:12 +0200
Processing by Rails::WelcomeController#index as HTML
  Parameters: {"foo"=>"bar"}
  Rendering /home/maxcal/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/railties-7.0.4/lib/rails/templates/rails/welcome/index.html.erb
  Rendered /home/maxcal/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/railties-7.0.4/lib/rails/templates/rails/welcome/index.html.erb (Duration: 0.1ms | Allocations: 15)
Completed 200 OK in 1ms (Views: 0.6ms | ActiveRecord: 0.0ms | Allocations: 300)


=> 200

app.response
包含响应对象。

请注意,您可能需要将 Grape API 服务器的主机添加到 Rails 配置中的白名单中。

© www.soinside.com 2019 - 2024. All rights reserved.