# How to get a POST data from in controller?

**URL:** <https://www.ruby-forum.com/t/how-to-get-a-post-data-from-in-controller/135271>\
**Category:** Rails\
**Created:** [April 20, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/how-to-get-a-post-data-from-in-controller/135271 "2008-04-20T12:23:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/how-to-get-a-post-data-from-in-controller/135271/1 "2008-04-20T12:23:33Z")

</div>

Hi, I am newbie in Ruby.  
Here is the situation I meet:  
I got a javascript app that will send string data by using HTTP POST  
method to another ruby app.  
Here is the url: [http://localhost:3000/home/receive\_data](http://localhost:3000/home/receive_data)  
So, the ruby app got a home controller and receive\_data action to  
handle it.  
It will write log when receive\_data action works just like the  
following code does:

def receive\_data  
trace “receive\_data…”  
render :text =\> “ok”  
end

But I never see logs appear when the javascript app send data.  
If I change the HTTP method of javascript app while sending data to  
GET, then it will get logs.  
It means the action in controller only can handle a request of GET  
method?  
How should I do, if I wanna receive data from POST method?

firestoke

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/how-to-get-a-post-data-from-in-controller/135271/2 "2008-04-20T14:18:34Z")

</div>

Hi,

normally Rails takes care of this automagically, and it doesn’t matter  
to an action whether the data comes in as GET or POST - in both cases,  
the data is accessible through the params method.

If you’re not getting anything in the logs, I would try to locate the  
problem rather within the calling javascript than within the rails  
app. If all else fails, have you tried POSTing to the action manually,  
via wget or with a hand-built HTML form?

Out of sheer interest, what’s ‘trace’? Sorry if this is an obvious  
question, but I am slightly hungover at the moment … 🙂

Jan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 21, 2008, 8:40am UTC](https://www.ruby-forum.com/t/how-to-get-a-post-data-from-in-controller/135271/3 "2008-04-21T08:40:50Z")

</div>

Sorry! I forget to explain the trace function. In fact, it works just  
like “puts” with a additional timestamp log at head. Here it is:

def trace(str = ‘’)  
p “[”+Time.new.to\_s(:db)+"] \*\*\*\*\* "+str.to\_s  
end

And I forget to say, it is a AJAX POST request from the javascript  
app.  
When I test it again today, it shows up the error like below (I didn’t  
alert it before):  
ActionController::InvalidAuthenticityToken in  
HomeController#receive\_data

After known what the error it is, I already found the solution in this  
article:  
[http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention](http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention)  
I override the verifiable\_request\_format? method in controller, then  
the receive\_data action works just as I expected.  
Thank for yor help any way! 🙂

firestoke
