Help Receiving a http POST to my application

Hi.. I'm just getting started with Ruby and am creating an API that
will receive POST requests from its users.

I've got a "fake" user sending POST requests to the right controller in
my application, but I can't seem to access the data being posted. (the
create method in my controller is being invoked...so I know I'm
addressing the POST call correctly)

The data being posted is:
name=johnny&amount=928339

How do I capture this in the controller, create a new record and
populate the name and amount fields in my database?

I don't want to use XML...my data must be in the above format.

Thanks!!!

Have you tried peeking at the params hash you get passed?

Fred

Teo Sor wrote:

Hi.. I'm just getting started with Ruby and am creating an API that
will receive POST requests from its users.

I've got a "fake" user sending POST requests to the right controller in
my application, but I can't seem to access the data being posted. (the
create method in my controller is being invoked...so I know I'm
addressing the POST call correctly)

The data being posted is:
name=johnny&amount=928339

How do I capture this in the controller,

The POST data should be parsed by Rails and made available through the
params[]-method:

params[:name]
params[:amount]

create a new record and
populate the name and amount fields in my database?

Model.create(...)

Christian Rishøj wrote:

Teo Sor wrote:

How do I capture this in the controller,

The POST data should be parsed by Rails and made available through the
params[]-method:

params[:name]
params[:amount]

create a new record and
populate the name and amount fields in my database?

Model.create(...)

I tried using the params method, but it seems to be empty (I used
params[:trans].inspect and it's empty (trans is my controller)). I even
tried using request.env['RAW_POST_DATA'] to see if there was anything
there...and I'm getting nothing!

I'm stumped!

If the POST is correctly landing on the server... try enabling debug
logging which should give you something like this in the logs:

Processing TransController#create (for ::1 at 2008-08-19 00:23:22)
[POST]
  Parameters: {"commit"=>"create", "name"=> "my name",
"action"=>"create", "controller"=>"trans"}

??

And the params would be:

logger.debug("[DEBUG] - This is the name param => #{params[:name]}")

This all depends however on the form/fake user that does the post...

One other thing to consider is whether the poster is setting the
correct mime type - rails will parse the parameters differently
depending if its a multipart form, xml, good old application/x-www-
form-urlencoded etc...

Fred

The name of the controller is unrelated to params. The keys in params
reflect their names, as seen in Christian's reply. You'd print
params.inspect.

Sorry that was half-edited. I meant the keys in params correspond to
the names of the parameters themselves.

Hey everyone thanks for all the help! I think I finally got it. I'm
not using a Ruby friendly post format, so I didn't want Ruby to assume a
post format and parse it into the hash automatically. In the end, I
used the following to get the raw post data...and now I'm going to parse
it out myself.

request.raw_post.to_s

Again...Thanks for the help!

T

Sounds like you found a workaround, but this is the way to do it:

Say for example the HTTP POST URL is www.mywebsite.com/messages/incoming?name=johnny&amount=928339

In the incoming method of the messages_controller.rb, you can access
these parameters as follows:

@variable1 = params[:name]
@variable2 = params[:amount]

-Gavin