I would like to print out response body generated by my app to stdout/stderr for debugging purposes. The traffic is server-server so I cannot use client tools to get hold of http traffic.
There is a mention of puts #response.body in http://api.rubyonrails.org/classes/ActionDispatch/Response.html, however in my app controller #response is undefined. Is there a way for me to print response body to logs in my rails app, and if so, how?
Based on the answer given, did it like this:
after_filter :print_response_body, :only => [:index]
def print_response_body
$stderr.puts response.body
end
In your controller, try
after_filter do
puts response.body
end
Related
I'm using service which sends Webhooks to my application. I want to write RSpec test for handling them. It's important to have this request exactly the same (remote caller IP, headers with encrypted content).
I tried to save request as json:
class WebhookController < ApplicationController
def some_callback
File.open('temp/request_example.json','w') do |f|
f.write request.to_json
end
end
end
so I could later do:
describe WebhookController do
subject { get :some_callback, JSON.parse(File.open('temp/request_example.json')) }
it 'does something' do;end
end
but unfortunately you cannot call request.to_json(request.to_json
IOError: not opened for reading). You can't either get directly to request.body or request.headers.
How to save such request for later usage in tests? Is there any gem for it?
With Rspec, I am trying to build a spec testing some basic http requests. I'm making a rookie mistake somewhere and need help finding it.
I am purposely making the spec fail with a nonsense expectation so the error message will tell me what I'm getting -- once I figure things out I'll correct the expectation:
user = create(:member)
json_data = {email: user.email, password: user.password}.to_json
post "api/v1/users/sign_in", json_data, format: :json
expect(last_response.body).to eq "foobar"
api/v1/users/sign_in routes to the following controller:
class API::V1::SessionsController < Devise::SessionsController
respond_to :json
def create
render text: params
end
end
This gives the error:
expected: "foobar"
got: "{\"{\\"email\\":\\"7abdiel_roob#smithrau.biz\\",\\"password\\":\\"12345678\\"}\"=>nil,
\"action\"=>\"create\", \"controller\"=>\"api/v1/sessions\"}"
Ok great. My data is getting to the server and the server sends it back, which is what I want. In my next step I try to grab the email. I change the controller to
class API::V1::SessionsController < Devise::SessionsController
respond_to :json
def create
render text: params[:email]
end
end
and I get
expected: "foobar"
got: " "
I looks to me that the params hash is using the JSOn data I sent in the request as the name of a key, not actually a value. Or maybe this is a strong_params thing? I've tried many things and can't seem to pull the data I want out of the params object.
What is happening is that you are double encoding the JSON data which you are sending in your spec.
json_data = {email: user.email, password: user.password}
post "api/v1/users/sign_in", json_data, format: :json
RSpec will automatically encode the request body as JSON for you.
I am coding in Ruby-on-Rails
I would like to send a http request to another service but not wait for a response.
Pseudocode:
def notification
require 'net/http'
...
# send net/http request
Net::HTTP.post_form(url, params)
render :text => "Rendered quickly as did not wait for response from POST"
end
Is there any way to send the POST request and not wait for a response and just to quickly render the page?
You can try delayed_job. It is mainly used to run processes in background. Once you install delayed_job you can do like this.
require 'net/http'
def notification
...
your_http_request #calling method
render :text => "Rendered quickly as did not wait for response from POST"
end
def your_http_request
# send net/http request
Net::HTTP.post_form(url, params)
#do your stuff like where to save response
end
handle_asynchronously :your_http_request
I am receiving http requests to my rails application to a url /account/postback
The body of this incoming request contains some json that I need to retrieve, how can I do this in ruby?
The following should print the body of the request
routes.rb
map.connect 'account/:action', :controller => 'accounts'
accounts_controller.rb
class AccountsController < ApplicationController
def postback
puts request.body.read
end
end
If your HTTP call is using the POST verb you could alternatively use request.raw_post to retrieve the contents sent in the request's body.
Hope it helps!
I'm getting a failing test here that I'm having trouble understanding. I'm using Test::Unit with Shoulda enhancement. Action in users_controller.rb I'm trying to test...
def create
unless params[:user][:email] =~ / specific regex needed for this app /i
# ...
render :template => 'sessions/new'
end
end
Test...
context 'on CREATE to :user' do
context 'with invalid email' do
setup { post :create, { 'user[email]' => 'abc#abcd' } }
should_respond_with :success
end
# ...
end
Fails because "response to be a <:success>, but was <302>". How is it 302?
Change action to...
def create
render :template => 'sessions/new'
end
Test still fails.
#Ola: You're wrong: POST is connected to create. PUT is normally connected to update.
A :forbidden is quiet odd though. Here are some suggestions to find the problem (I've never used Shoulda, but I don't think it is a problem with Shoulda.
Make sure the route is defined in config/routes.rb and check with rake routes
Do you have any before_filters that could be responsible for that behaviour (login filter, acts_as_authenticated etc..)? Checkout log/test.log. A halt in the filter chain shows up there.
Print out the response body puts response.body to see what you get returned.
Hope this helps.
If you're using default REST-ful URLs, you probably should use PUT, not POST... Since PUT is connected to create, POST to that URL will give you an unauthorized and redirect.