[Slightly OT] POSTing data to a Rails App

Hi, I have a Windows program that collects data and would like to store
this data in a database that is usually accessed through Rails.

It was recommended that I try to “POST” the data to the Rails
application. I’m running into some problems trying to format my POST
string for the application. I’m trying to learn by interfacing to the
TODO application (Four Days with Rails) with Borland C++ Builder that
has some components for HTTP access.

According to the application, to create a new category, I need to post
to “http://localhost/categories/create” (that seems to work fine)
For the POST string, I’m using “category_category=newCat” - this seems
to have a problem cos the server returns a packet saying that it was
unable to save the data - “Category is too short (minimum is 1
characters)”

The form to which I am posting has the following:

Category:

Any idea on how the string should be formatted? Do I need to include
anything about the Submit button?

I’ve spent quite a while trying to search the net but to no avail - the
items in my search (like post string) are too generic to get any
meaningful results…

Thanks,
Mohit.

http://localhost/categories/create” (that seems to work fine)
size=“20” type=“text” value="" />


Any idea on how the string should be formatted? Do I need to include
anything about the Submit button?

You want ‘category[category]’ not ‘category_category’ (ie. you want the
name attribute, not the id attribute)…

-philip

http://localhost/categories/create” (that seems to work fine)
size=“20” type=“text” value="" />

the server has received?
Check the appropriate log file (development or production). It will
tell
you what it thinks the incoming paramters are…

Try:

or even better:

<%= form_tag :action => :create, :method => ‘post’ %>

Otherwise, you need to post your logs.

        - dan


Dan K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000

Philip H. wrote:

post to “http://localhost/categories/create” (that seems to work fine)

<input id="category_category" maxlength="20" anything about the Submit button?

You want ‘category[category]’ not ‘category_category’ (ie. you want
the name attribute, not the id attribute)…

-philip

Hi Philip,

Thanks for the quick response. I should have mentioned that I did try
both. Both options fail with the same error. I guess I should try and
see what is actually being sent to the server. Is there any way to see
what POST string the server has received?

Cheers
Mohit.

Hi Philip,

Thanks for the quick response. I should have mentioned that I did
try both. Both options fail with the same error. I guess I should
try and see what is actually being sent to the server. Is there any
way to see what POST string the server has received?

Check the appropriate log file (development or production). It will
tell you what it thinks the incoming paramters are…

Hi! Thanks again! In the meanwhile, I added some code into the flash to
see the RAW_POST_DATA that was received from the browser (when it’s
working) and this is what it gives me - category%5Bcategory%5D=Cat1

The corresponding line in the development log is:
Parameters: {“category”=>{“category”=>“Cat1”}, “action”=>“create”,
“controller”=>“categories”}

When my program submits data, this is what the log shows…
Parameters: {“action”=>“create”, “controller”=>“categories”,
“category%5Bcategory%5D=newCat”=>""}

Not sure what to do now :-S

Thanks
Mohit.

Dan K. wrote:

       - dan

Hi Dan,

Thanks for the reply. The Rails interaction with a browser works fine
and is in fact created using the better style (the second one in your
example above).

My problem is that I am trying to post the data to it not from a web
browser but instead another program that is collecting it. The program
collects the data and is trying to “behave” like a browser by submitting
an HTTP POST request. In this request, I’m confused about what the
exact POST string should be.
My current string is: category%5Bcategory%5D=newCat
and shows up in the logs as:
Parameters: {“action”=>“create”, “controller”=>“categories”,
“category%5Bcategory%5D=newCat”=>""}
while the post from the browser is: category%5Bcategory%5D=Cat1
and shows in the logs as:
Parameters: {“category”=>{“category”=>“Cat1”}, “action”=>“create”,
“controller”=>“categories”}

Trying to figure out what to do…
Cheers
Mohit.

Thanks
Mohit.

I’d recommend using the Live HTTP Headers extension for Firefox. It
will let you drop down a level lower and see exactly what is being
transmitted in the HTTP transactions by the browser and by your
client. One possibility is that you’re not setting the MIME type
correctly, but the HTTP logs will show you exactly what’s different.

        - dan


Dan K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000

Mohit S. wrote:

Thanks for the reply. The Rails interaction with a browser works fine
and is in fact created using the better style (the second one in your
example above).

My problem is that I am trying to post the data to it not from a web
browser but instead another program that is collecting it. The program
collects the data and is trying to “behave” like a browser by submitting
an HTTP POST request. In this request, I’m confused about what the
exact POST string should be.
My current string is: category%5Bcategory%5D=newCat
and shows up in the logs as:
Parameters: {“action”=>“create”, “controller”=>“categories”,
“category%5Bcategory%5D=newCat”=>""}
while the post from the browser is: category%5Bcategory%5D=Cat1
and shows in the logs as:
Parameters: {“category”=>{“category”=>“Cat1”}, “action”=>“create”,
“controller”=>“categories”}

Trying to figure out what to do…

It is probably a Form encoding problem.

http://www.webreference.com/html/tutorial13/4.html

My advice, look at the simply_restful plugin and post the data as Xml. I
can’t give any code samples because I haven’t actually done this yet
using Rails. But I hope this gets you going in a simpler direction
anyway.

Matt G.

Dan K. wrote:

I’d recommend using the Live HTTP Headers extension for Firefox. It
will let you drop down a level lower and see exactly what is being
transmitted in the HTTP transactions by the browser and by your
client. One possibility is that you’re not setting the MIME type
correctly, but the HTTP logs will show you exactly what’s different.

       - dan

–Dan K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000

Hi Dan & Philip,

Thanks for your inputs. I tried a number of things (rather uselessly)
and then switched to using a different component for submitting the data
via a POST HTTP action to the server. It works now - you were right, it
was indeed a

Now on to more interesting things
1- stitching together all the parameters that need to be submitted (in
my C++ program)
2- understand the response to make sure it succeeded (in my C++ program)
3- seeing if it’s possible to create a different kind of response for
POSTs received from this program (on the Rails sides)
4 - working through authentication

As regards #3, I read about the “.xhr?” method to check if the request
was an AJAX request and also for using responds_to. Should I use a
‘custom’ format that it accepts to distinguish between requests from my
application and other requests from the web? Or is there some other
cleaner way to deal with it?

Cheers
Mohit.

What was the actual issue?

I would just add a field to your post, client = c++ version 0.9. You
could even use a custom HTTP User Agent header. I think .xhr is
confusing paradigms for no reason.

        - dan


Dan K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000

Dan K. wrote:

What was the actual issue?

I would just add a field to your post, client = c++ version 0.9. You
could even use a custom HTTP User Agent header. I think .xhr is
confusing paradigms for no reason.

       - dan 

Hi Dan,

Thanks for hanging in there :slight_smile:

I should have pointed this out, my apologies! The actual issue seems to
have been with the C++ component that I was using. I tried a number of
combinations and tried to even trace the actual headers that were sent.
I found that the size of the “content” was always larger by a few bytes
than what I expected (this wasn’t clearly documented in the help files,
so I don’t know what it’s doung!) I think it was doing some encoding
that was messing up things. I did eventually do a search related to the
component and found that many people had faced a similar problem… and
recommended migrating to another freeware component instead. When I
switched to the new component and followed the example, it worked just
fine! (Actually, it gives me another error but that I can solve!)

I do not want to go down the .xhr path to create different results for
my program - I am more keen to use either “Accepts” or perhaps another
field for this job. Actually, using another field seems to be the
easier and more elegant solution… I’d prefer to avoid having to
receive HTML in the C++ program and then try to figure out the actual
codes embedded in the HTML… :slight_smile:

What do you guys think about using a separate action in the
controller… so, I post to “/categories/create_from_pcclient” instead
of “/categories/create”? I’m not sure how I can prevent it being
accessed from the web in that case…

Thanks… if you have any suggestions, do send in :slight_smile:

Now on to more interesting things
1- stitching together all the parameters that need to be submitted
(in my C++ program)
This should be simple enough to do :slight_smile:

2- understand the response to make sure it succeeded (in my C++ program)
Depends on solution to #3

3- seeing if it’s possible to create a different kind of response for
POSTs received from this program (on the Rails sides)
Thanks for the inputs…

4 - working through authentication
Hmmm… I think this needs cookies… I have to see what’s required from
my C++ program to maintain user sessions!

Anyway, I’m enjoying this project! :slight_smile:
Cheers
Mohit.

If the controller methods do the same thing, I’d avoid repeating myself.
I
think a user-agent header should be easy to set and check for.

… Original Message …
On Fri, 21 Jul 2006 00:45:44 +0800 “Mohit S.” [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs