How can I debug a PUT request in Rails 3.1? - mysql

I am developing a simple iOS app, which uses a Rails app as a server backend. I'm using the Restkit framework for the management of the server networking / requests and on the iOS side all seems to be OK.
When I make a PUT request, I get a 200 response back, and the logs in XCode seem to suggest all is well. When I look at the logs for the Rails app, it also seems to suggest all is well, with the following output:
2011-12-19T18:15:17+00:00 app[web.1]: Started PUT "/lists/3/tasks/6" for 109.156.183.65 at 2011-12-19 18:15:17 +0000
2011-12-19T18:15:17+00:00 app[web.1]: Parameters: {"created_at"=>"2011-12-12 22:37:00 +0000", "id"=>"6", "updated_at"=>"2011-12-12 22:37:00 +0000", "description"=>"Create a home page", "list_id"=>"3", "completed"=>"1"}
2011-12-19T18:15:17+00:00 app[web.1]: Task Load (4.3ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = $1 LIMIT 1 [["id", "6"]]
2011-12-19T18:15:17+00:00 app[web.1]: Processing by TasksController#update as JSON
2011-12-19T18:15:17+00:00 app[web.1]: (4.7ms) BEGIN
2011-12-19T18:15:17+00:00 app[web.1]: (1.5ms) COMMIT
2011-12-19T18:15:17+00:00 app[web.1]: Completed 200 OK in 48ms (Views: 1.1ms | ActiveRecord: 16.0ms)
2011-12-19T18:15:17+00:00 heroku[nginx]: 109.156.183.65 - - [19/Dec/2011:10:15:17 -0800] "PUT /lists/3/tasks/6 HTTP/1.1" 200 154 "-" "TaskM8/1.0 CFNetwork/485.13.9 Darwin/11.2.0" taskm8.com
2011-12-19T18:15:17+00:00 heroku[router]: PUT taskm8.com/lists/3/tasks/6 dyno=web.1 queue=0 wait=0ms service=114ms status=200 bytes=154
However, when I make another get request, or use the standard web views to look at the data, the change I was expecting from the PUT request (Completed = 1 - which is a BOOL field), no change has been made.
I can see from the rails log that my iOS app is passing the correct parameters, so it seems to be something on the rails side. I've been through the loop of overcoming the CSRF error message, so don't think it's that.
On a local version of the rails app, I've also run general logging against the MySql database to monitor the queries being run, trying to see if the PUT does anything at all, or anything which would fail... in the log, you don't see anything other than:
BEGIN
COMMIT
The same as the rails log.
So, does anyone have any idea about why the PUT is not making the changes to the data, or how I can debug the PUT further?
Apologies if this is a real simple question, I'm slowly getting back into development and am somewhat rusty!

Are you using any kind of automated tests? If not, start there.
Another way (though rubbish) would be to call your controller action from a webpage and see if it works.
You can also add logger.debug in your rails code to add traces.

If you have control over the MySQL server I would suggest that you enable the general log aka the query log, that way you can see what really happens.
SET GLOBAL general_log_file = '/tmp/query.log'
SET GLOBAL general_log = 'ON';

Related

Coinbase callback returning a '422 Unprocessable Entity' after POST - Rails 4

I'm trying to set up methods to handle callbacks from Coinbase for my app. When I send test callbacks via a POST, my "Server responded with: 422 Unprocessable Entity".
The callback/POST is just sending a simple JSON and expecting a status 200 back. I've tried to make things uber simple first just for testing. This is what I have in my controller:
def callbacks
render json: {status: "OK"}
end
But even with that, I'm getting this in my server console logs:
2014-10-10T16:31:30.318374+00:00 app[web.1]: Started POST "/contribute/callbacks for 173.245.56.73 at 2014-10-10 09:31:30 -0700
2014-10-10T16:31:30.323217+00:00 app[web.1]: Parameters: {"order"=>{"id"=>nil, "created_at"=>nil, "status"=>"completed", "event"=>nil, "total_btc"=>{"cents"=>100000000, "currency_iso"=>"BTC"}, "total_native"=>{"cents"=>35925, "currency_iso"=>"USD"}, "total_payout"=>{"cents"=>35925, "currency_iso"=>"USD"}, "custom"=>"123456789", "id"=>nil}, "transaction"=>{"id"=>"54380", "hash"=>"4a5e1b", "confirmations"=>0}}}}
2014-10-10T16:31:30.324629+00:00 app[web.1]: Completed 422 Unprocessable Entity in 1ms
2014-10-10T16:31:30.323041+00:00 app[web.1]: Processing by PaymentsController#callbacks as JSON
2014-10-10T16:31:30.324325+00:00 app[web.1]: Can't verify CSRF token authenticity
I would really appreciate any help. I have also tried variations of render. Dont know what to do next to troubleshoot.

http request fails due to json size - response 503 and Heroku error H13

I have a simple flask application running on heroku:
from flask import Flask
app = Flask(__name__)
#app.route('/test', methods=['POST'])
def test():
return 'OK'
Incredibly it succeeds or fails, depending on the size of the json I send. Here's the test code:
import json, random, string, requests
def rand_string(size):
return ''.join([random.choice(string.letters) for i in xrange(size)])
for size in (4000, 10000):
r = requests.post('http://my-app.herokuapp.com/test',
data=json.dumps(rand_string(size)),
headers={'content-type': 'application/json'})
print r.status_code
On the first call it returns status 200, on the second http status 503 with a H13 heroku error code.
2014-09-18T08:23:46.594543+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/test" host=my-app.herokuapp.com request_id=91b1cd91-5a4f-445e-ad52-3c64733154b3 fwd="12.34.56.78" dyno=web.1 connect=5ms service=13ms status=503 bytes=0
HTTP response 503 means:
server is currently unable to handle the request due to a temporary
overloading or maintenance of the server
This couldn't be the case, as the server is up and running and has no load except for my manual tests.
Heroku's H13 code documentation says:
This error is thrown when a process in your web dyno accepts a
connection, but then closes the socket without writing anything to it.
However all the code does is return 'OK' so it ain't the code.
Does heroku limit the size of requests? Does gunicorn?
How can I find out and how do I configure it otherwise?
You may have been affected by gunicorn's timeout mechanism, which defaults to 30s. If serializing for some reason takes longer than that, the worker is killed and restarted.

Can't verify CSRF token authenticity and email can't be blank

Context:
I'm playing with a Jquery Mobile app and a Rails backend server. I create users from the app, through a POST Ajax call. The back end is, by all purposes for the mobile app, just an API.
I've been all evening reading about this CSRF token authenticity issue, and cannot find how to kill it. I'm on Rails 4 and Devise 3.1 (with token_authenticatable module)
This is the log on console when I try to save a user through the app
Started POST "/users.json" for IP at 2013-09-28 00:05:17 +0200
Processing by RegistrationsController#create as JSON
Parameters: {"user"=>{"username"=>"seba", "email"=>"mail#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
Can't verify CSRF token authenticity
user:
(0.1ms) begin transaction
(0.1ms) rollback transaction
Completed 422 Unprocessable Entity in 9ms (Views: 0.3ms | ActiveRecord: 0.2ms)
I read on this site, that if I put the following line
skip_before_filter :verify_authenticity_token
in my custom Registration controller, the verification error would be gone. It does, but now there's a warning:
Started POST "/users.json" for IP at 2013-09-28 02:38:16 +0200
DEPRECATION WARNING: devise :token_authenticatable is deprecated. Please check Devise 3.1 release notes for more information on how to upgrade. (called from <class:User> at /home/seba/repos/elsapo/app/models/user.rb:4)
Processing by RegistrationsController#create as JSON
Parameters: {"user"=>{"username"=>"seba", "email"=>"mail#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
user:
(0.1ms) begin transaction
(0.2ms) rollback transaction
Completed 422 Unprocessable Entity in 65ms (Views: 0.4ms | ActiveRecord: 1.5ms)
The line user: is the logger.debug, to see if I'm storing something.
In both cases, my app continues to show the same message after submitting the date: "email can't be blank" and then "password can't be blank"
Any pointers or suggestions? I anyone needs more information, I'll gladly deliver it.

SQL Insert committed but not found on next request?

I have placed an after_commit callback in the RequestToken model that outputs "Committed Request Token xx". You can see in the log I included below, that the token record is committed and the next request the lookup on the object says it cannot be found. The issue occurs intermittently and if I refresh the page the record is found and the request goes through.
Environment
AWS EC2 + RDS, Ubuntu 10.04, Rails 3.2.8, MySQL2 0.3.11 gem, apache2 2.2.14, phusion passenger 3.0.11
Has anyone seen this before? Any suggestions?
Committed Request Token S8j311QckvEjnDftNW0e7FPHsavGWTelONcsE3X1
Rendered text template (0.0ms)
Completed 200 OK in 28ms (Views: 0.6ms | ActiveRecord: 21.8ms | Sphinx: 0.0ms)
Started GET "/oauth/authorize?oauth_token=S8j311QckvEjnDftNW0e7FPHsavGWTelONcsE3X1" for 96.236.148.63 at 2012-10-15 22:07:32 +0000
Processing by OauthController#authorize as HTML
Parameters: {"oauth_token"=>"S8j311QckvEjnDftNW0e7FPHsavGWTelONcsE3X1"}
Completed 500 Internal Server Error in 5ms
ActiveRecord::RecordNotFound (Couldn't find RequestToken with token = S8j311QckvEjnDftNW0e7FPHsavGWTelONcsE3X1):
200 Doesn't mean it saved. Probably failed a validation.

Beginner Ruby / MSQL Error

http://localhost:3000/genre
I'm using Instant Rails, I'm introducing scaffolding for the first time ever.
So everything looks like its working fine, I did my own RoR page etc, today i come in today and when I go to localhost:3000 I get the following error message:
Oops! Google Chrome could not connect to localhost:3000
Any suggestions of what I should check would be greatly appreciated. So far I know to confirm that:
the windows host file connects 127.0.0.1 to localhost
I've had an update to this the Mongrel server doesnt start properly with the new app I have created, rather than an open window, it just runs a script and closes the window without confirmation that it has started.
Thanks in advance.
here is the details of the Mongrel error:
C:\Users\Will\Desktop\instantrails\rails_apps\talewiki>mongrel_rails start -p 30
00
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.
2/lib/active_record/connection_adapters/abstract/connection_specification.rb:217
:in establish_connection': development database is not configured (ActiveRecord
::AdapterNotSpecified)
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/gems/1.8/gems/acti
verecord-2.0.2/lib/active_record/connection_adapters/abstract/connection_specifi
cation.rb:208:inestablish_connection'
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/gems/1.8/gems/rail
s-2.0.2/lib/initializer.rb:234:in initialize_database'
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/gems/1.8/gems/rail
s-2.0.2/lib/initializer.rb:94:inprocess'
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/gems/1.8/gems/rail
s-2.0.2/lib/initializer.rb:49:in send'
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/gems/1.8/gems/rail
s-2.0.2/lib/initializer.rb:49:inrun'
from C:/Users/Will/Desktop/instantrails/rails_apps/talewiki/config/envir
onment.rb:13
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/site_ruby/1.8/ruby
gems/custom_require.rb:27:in gem_original_require'
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/site_ruby/1.8/ruby
gems/custom_require.rb:27:inrequire'
... 9 levels...
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/gems/1.8/gems/mong
rel-1.1.2-x86-mswin32/bin/../lib/mongrel/command.rb:212:in run'
from C:/Users/Will/Desktop/instantrails/ruby/lib/ruby/gems/1.8/gems/mong
rel-1.1.2-x86-mswin32/bin/mongrel_rails:281
from C:/Users/Will/Desktop/instantrails/ruby/bin/mongrel_rails:19:inlo
ad'
from C:/Users/Will/Desktop/instantrails/ruby/bin/mongrel_rails:19
As it appears your Database has not been configured. Check out the APP_ROOT/config/database.yml file and find if you have created the database that corresponds to the development environment. Once you setup the DB, your code should run fine.