Rails:如何检查使用“呈现json:”创建的响应?

时间:2022-10-08 14:10:19

I am trying to understand the mechanics of asynchronously updating Rails views by rendering JSON (as my boss wants it done that way).

我试图通过呈现JSON来理解异步更新Rails视图的机制(正如我老板希望的那样)。

Not very successful so far.

到目前为止还不是很成功。

Is there a way to inspect the response as it is seen on the OS layer? Like when I use curl? I am trying to write up my learnings for a possible blog post about this topic and having a way to visualize what Rails sends out when rendering JSON would be really helpful.

是否有一种方法可以检查操作系统层上的响应?比如我用卷发?我正在尝试为一个可能的关于这个主题的博客文章写我的经验,并且有一种方法来可视化Rails在呈现JSON时发送的内容,这将非常有帮助。

The important controller part, looks like this:

重要的控制器部分是这样的:

def create
  @order = Order.find_by(id: params[:order_id])
  @comment = current_user.comments.new(comment_params)
  .
  .
  return unless @comment.save!

  respond_to do |format|
    format.json { render json: @comment, context: self }
  end

end

EDIT:

编辑:

Based on one of the comments I tried inspecting the response with the debugger/pry, first making sure the @comment object contains data:

基于其中一条注释,我尝试使用调试器/pry检查响应,首先确保@comment对象包含数据:

(byebug) @comment
#<Comment id: 3090, order_id: 125, user_id: 18, content: "asdfad", created_at: "2017-02-01 12:21:25", updated_at: "2017-02-01 12:21:25">

Seems good.

看起来很好。

(byebug) response.body
""

Not so cool, where is the JSON data? Why is the body empty?

不太酷,JSON数据在哪里?为什么身体是空的?

2 个解决方案

#1


1  

The approach described above using a spec is totally valid and good. Also you then already have specs in place :)

上面描述的使用规范的方法是完全有效和良好的。你也已经有了规范:)

Anyway one may face some other valid use-cases (there maybe more than the once below) where one needs to inspect requests / responses.

无论如何,我们可能会遇到一些其他有效的用例(可能不止一次),在这些用例中我们需要检查请求/响应。

1. perform some request with javascript to a local / current application

1。使用javascript对本地/当前应用程序执行一些请求

One can use e.g. the chrome web dev tools to inspect the request, response and headers. This is very useful and handy.

可以使用chrome web开发工具来检查请求、响应和标题。这是非常有用和方便的。

Also you can tail your application log and see the incoming request with params. In rails development you will also see a lot of rails debug information such as active record statements, render calls, mailer interaction and so on.

您还可以跟踪您的应用程序日志,并使用params查看传入的请求。在rails开发中,您还将看到大量的rails调试信息,如活动记录语句、呈现调用、mailer交互等等。

In addition to the information provided you can add as much debug output (assuming you are in rails development mode) with

除了提供的信息之外,您还可以添加更多的调试输出(假设您处于rails开发模式中)。

logger.debug "any important information you need"

or even debug with breakpoints [1].

或者使用断点[1]进行调试。

2. perform some request with javascript to a remote / other application

2。使用javascript对远程/其他应用程序执行一些请求

In case you have control over the remote application then this is basically them as 1. You just tail the log of the other application. :)

如果你能控制远程应用程序,这基本上就是1。您只需跟踪另一个应用程序的日志。:)

3. perform a request from within a rails controller to another application

3所示。从rails控制器中执行对另一个应用程序的请求

In case you have control over the other application you can again tail its log, debug with breakpoints or add log output (e.g. from the @request variable which is available in controller level)

如果您控制了另一个应用程序,您可以再次跟踪它的日志、使用断点进行调试或添加日志输出(例如,从控制器级可用的@request变量)

Also you can use debug logging in the main application and output the response received or debug right in that spot.

还可以在主应用程序中使用调试日志记录,并输出在该位置接收或调试的响应。

If your are using some http client as faraday [2] (which I can highly recommend) you can active its logging. See the basic use chapter [3]. With faraday you can even provide your own logger or use some 3rd party logger [4] to get nicer outputs.

如果您正在使用一些http客户端作为faraday[2](我强烈推荐),您可以激活它的日志记录。参见基本使用章节[3]。使用法拉第,你甚至可以提供你自己的记录器,或者使用第三方记录器[4]来获得更好的输出。

[1] http://guides.rubyonrails.org/debugging_rails_applications.html

[1]http://guides.rubyonrails.org/debugging_rails_applications.html

[2] https://github.com/lostisland/faraday

[2]https://github.com/lostisland/faraday

[3] https://github.com/lostisland/faraday#basic-use

[3]https://github.com/lostisland/faraday基本用途

[4] https://github.com/envylabs/faraday-detailed_logger

[4]https://github.com/envylabs/faraday-detailed_logger

#2


2  

You can write a test to achieve your goal or you can put the code after_action {puts response.body } to a controller.

您可以编写一个测试来实现您的目标,或者您可以将代码after_action {put response。控制器的body}。

application_controller.rb

application_controller.rb

class ApplicationController < ActionController::Base
  ...
  after_action { puts response.body }
  ...
end

some_test_spec.rb

some_test_spec.rb

require 'rails_helper'

RSpec.describe YourController, type: :controller do

  describe "GET #index" do

    it "returns some data" do
      get :index

      puts response.body

      binding.pry # for interactive debugging

      expect(response.status).to eq(200)
    end

  end

end

#1


1  

The approach described above using a spec is totally valid and good. Also you then already have specs in place :)

上面描述的使用规范的方法是完全有效和良好的。你也已经有了规范:)

Anyway one may face some other valid use-cases (there maybe more than the once below) where one needs to inspect requests / responses.

无论如何,我们可能会遇到一些其他有效的用例(可能不止一次),在这些用例中我们需要检查请求/响应。

1. perform some request with javascript to a local / current application

1。使用javascript对本地/当前应用程序执行一些请求

One can use e.g. the chrome web dev tools to inspect the request, response and headers. This is very useful and handy.

可以使用chrome web开发工具来检查请求、响应和标题。这是非常有用和方便的。

Also you can tail your application log and see the incoming request with params. In rails development you will also see a lot of rails debug information such as active record statements, render calls, mailer interaction and so on.

您还可以跟踪您的应用程序日志,并使用params查看传入的请求。在rails开发中,您还将看到大量的rails调试信息,如活动记录语句、呈现调用、mailer交互等等。

In addition to the information provided you can add as much debug output (assuming you are in rails development mode) with

除了提供的信息之外,您还可以添加更多的调试输出(假设您处于rails开发模式中)。

logger.debug "any important information you need"

or even debug with breakpoints [1].

或者使用断点[1]进行调试。

2. perform some request with javascript to a remote / other application

2。使用javascript对远程/其他应用程序执行一些请求

In case you have control over the remote application then this is basically them as 1. You just tail the log of the other application. :)

如果你能控制远程应用程序,这基本上就是1。您只需跟踪另一个应用程序的日志。:)

3. perform a request from within a rails controller to another application

3所示。从rails控制器中执行对另一个应用程序的请求

In case you have control over the other application you can again tail its log, debug with breakpoints or add log output (e.g. from the @request variable which is available in controller level)

如果您控制了另一个应用程序,您可以再次跟踪它的日志、使用断点进行调试或添加日志输出(例如,从控制器级可用的@request变量)

Also you can use debug logging in the main application and output the response received or debug right in that spot.

还可以在主应用程序中使用调试日志记录,并输出在该位置接收或调试的响应。

If your are using some http client as faraday [2] (which I can highly recommend) you can active its logging. See the basic use chapter [3]. With faraday you can even provide your own logger or use some 3rd party logger [4] to get nicer outputs.

如果您正在使用一些http客户端作为faraday[2](我强烈推荐),您可以激活它的日志记录。参见基本使用章节[3]。使用法拉第,你甚至可以提供你自己的记录器,或者使用第三方记录器[4]来获得更好的输出。

[1] http://guides.rubyonrails.org/debugging_rails_applications.html

[1]http://guides.rubyonrails.org/debugging_rails_applications.html

[2] https://github.com/lostisland/faraday

[2]https://github.com/lostisland/faraday

[3] https://github.com/lostisland/faraday#basic-use

[3]https://github.com/lostisland/faraday基本用途

[4] https://github.com/envylabs/faraday-detailed_logger

[4]https://github.com/envylabs/faraday-detailed_logger

#2


2  

You can write a test to achieve your goal or you can put the code after_action {puts response.body } to a controller.

您可以编写一个测试来实现您的目标,或者您可以将代码after_action {put response。控制器的body}。

application_controller.rb

application_controller.rb

class ApplicationController < ActionController::Base
  ...
  after_action { puts response.body }
  ...
end

some_test_spec.rb

some_test_spec.rb

require 'rails_helper'

RSpec.describe YourController, type: :controller do

  describe "GET #index" do

    it "returns some data" do
      get :index

      puts response.body

      binding.pry # for interactive debugging

      expect(response.status).to eq(200)
    end

  end

end