How to debug an HTTP POST call to a Ruby on Rails API

I am writing an API in Ruby on Rails which accepts POST requests that send data in json format. I would like to use RubyMine to debug the application from the time the POST request is sent. It is unclear to me how to create a debug configuration to accomplish this.

I found searching the discussion archive that PHPStorm has a PHP HTTP Request configuration. I am not seeing anything like this in RubyMine?

If this option does not exist in RubyMine, I would appreciate any guidance as to how else to use RubyMine to debug HTTP POST requests sent to a Rails application (or somehow fake POST requests and the JSON input, so that I can still use RubyMine's debugger).

10 comments
Comment actions Permalink

Hi,

I'm not sure I understand what kind of debugging do you need :(
Do you want to debug some ruby code or do you want an advice on how to send POST to your Rails app?
Could you please clarify.

Thanks in advance, Oleg.

0
Comment actions Permalink

Oleg, thank you for responding. I can send an HTTP POST request to the API just fine, be it via Terminal, an HTML form, etc. What I am trying to accomplish is use the RubyMine debugger to step through each line of code, see the value of variables, etc, i.e. use the RubyMine Debugger, from the moment the Rails application receives the POST request.

I understand that one can select a file in RubyMine and choose to debug it. Similarly, with the Ruby command line debugger, one can specify a file to debug. But I don't have a file per se that I can choose in this case, as the trigger action is the Rails based application receiving an HTTP POST request.

So what I would like to do is launch the RubyMine debugger under the same conditions as when an HTTP POST request is received by the application. So either when an HTTP POST request is actually received, or somehow recreate a similar environment that would allow me to use the RubyMine debugger.

Please let me know if this is still unclear.

0
Comment actions Permalink

Hi Marcelo,

our debugger allows to run any ruby script to debug, in you case I think you should run Rails server under debugger set some breakpoints in places you want to debug.
Let me know if this helps or feel free to ask more information.

Regards, Oleg.

0
Comment actions Permalink

I am not sure that we are on the same page as to what I am trying to accomplish, and how to accomplish, if possible at all. How do you "run Rails server under debugger"? That in essence is my question. Would you please specify the steps to follow to have the debugger launch when an HTTP POST request is received? What kind of debug configuration would I create?

0
Comment actions Permalink

Ok, let's start from the beginning.  How do you run your service (w/o debugger) from command line (or RubyMine)

Regards, Oleg.

0
Comment actions Permalink

I can send the HTTP POST request in various forms, I think the most useful one would be via an Rspec unit test, which reads as follows:

  it "returns expected yield for test case" do
    post "/api/yield", File.read("lib/test_scenarios/input/yield_input_cross4010KOx1700Het_target4010Het1700Het.json"),
         { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json' }
    response.body.should    eql({ :expectedYieldForBreeding => 0.5}.to_json)
    response.status.should be(200)
  end

Then my routes.rb handles this post by sending it to a controller:

namespace :api, defaults: { format: 'json' } do

    match 'yield' => 'yield#scenario', :via => :post
    match 'yield' => 'yield#scenario', :via => :get
    match 'projection' => 'projection#scenario', :via => :post

  end

And then the controller does something with it.

I would like to step through the code, using the RubyMine debugger, at the latest when the scenario method in the yield controller is called.

I don't know how to make the debugger "kick in" when the Rails application receives the POST call.

0
Comment actions Permalink

So, am I right that you can run the rspec test from RubyMine? Am I right?
If so you can just set breakpoint inside controller's method which will be called and debug this run configuration (Run|Debug).

Regards, Oleg.

0
Comment actions Permalink

Oh boy, you are right. That works and that gives me what I need now.

Let me ask you: if the HTTP POST request is sent by a web browser lets say, not via a Ruby file that I can select in RubyMine to "debug as", is it possible to have the RubyMine debugger launch automatically somehow, upon the Rails application receiving a POST request from a web browser? That would be so I can debug not from the context of a unit test, but from the context of a user sending the POST request via a web browser.

Thank you very much for answering my original question, which I will mark as answered now, and I look forward to your thoughts regarding the question in the paragraph above.

0
Comment actions Permalink

There is no concept of launching debugger up on some event, you just run test/rake/rails server UNDER debugger, set breakpoint in some places
and debugger will stop execution in these places.  So  you just need to know how to run test/rake command/rails serer in RubyMine and use the same
run configuration to debug it.

Regards, Oleg.

0
Comment actions Permalink

I see. I don't fully grasp what that means at this point - I am very new to Ruby and Rails - but I will research it. Thank you very much Oleg!

0

Please sign in to leave a comment.