Parsing XML POST params

We’ve just upgraded our Rails 2.0.2 app to 2.3 and the webservices
create APIs are failing. Instead of receiving parsed XML, the
controller’s create method gets the whole xml as a hash key with the
value nil.

To isolate this issue, I generated a vanilla app:
$ rails scaffold_xml
$ ./script/generate scaffold project title:string description:text
$ rake db:migrate

In real life I test this with cucumber features, but for the purposes of
short repro steps, I added this to the top of projects_controller.rb
skip_before_filter :verify_authenticity_token

And then ran the POST for the create request on the command line
$ curl -X POST -d ‘AwesomeThis is
an awesome project.’ http://localhost:3000/projects.xml

<?xml version="1.0" encoding="UTF-8"?> 2009-06-21T14:09:08Z 1 2009-06-21T14:09:08Z

The log says:
Processing ProjectsController#create to xml (for 127.0.0.1 at 2009-06-21
07:09:08) [POST]
Parameters: {“AwesomeThis is an
awesome project.”=>nil}
Project Create (10.4ms) INSERT INTO “projects” (“updated_at”,
“title”, “description”, “created_at”) VALUES(‘2009-06-21 14:09:08’,
NULL, NULL, ‘2009-06-21 14:09:08’)
Completed in 181ms (View: 134, DB: 10) | 201 Created
[http://localhost/projects.xml]

To test my assumptions, I repeated the above steps with “rails 2.0.2
scaffold_xml_202” to look at it in the old version of rails (minus the
forgery bit which didn’t used to be required.) I must be missing
something basic here. Can anyone enlighten me or point me to relevant
docs?

Thanks,
Sarah

Sarah A. wrote:

To test my assumptions, I repeated the above steps with “rails 2.0.2
scaffold_xml_202” to look at it in the old version of rails (minus the
forgery bit which didn’t used to be required.)

Note: in Rails 2.0.2 it does the exact same thing, so I must need to do
something special to get the behavior I want, or some assumption I’m
making about the default webservices XML API behavior is incorrect.

Sarah

On Jun 21, 3:26 pm, Sarah A. [email protected]
wrote:

In real life I test this with cucumber features, but for the purposes of
short repro steps, I added this to the top of projects_controller.rb
skip_before_filter :verify_authenticity_token

And then ran the POST for the create request on the command line
$ curl -X POST -d 'AwesomeThis is
an awesome project.'http://localhost:3000/projects.xml

I assume this is a typo and than in the real world you had remembered
the closing . Having done that you need to get curl to set
the content type appropriately.

Fred

Frederick C. wrote:

I assume this is a typo and than in the real world you had remembered
the closing . Having done that you need to get curl to set
the content type appropriately.

I thought that posting to projects.xml would mean that setting the
content-type wasn’t required (even though I am doing that in cucumber,
just cuz it seems like the right thing to do)

Sarah

Frederick C. wrote:

I assume this is a typo and than in the real world you had remembered
the closing . Having done that you need to get curl to set
the content type appropriately.

Ah, so there must be a bug in my test. This works:

$ curl -X POST -d ‘AwesomeThis is
an awesome project.’ -H “Content-Type:
application/xml” http://localhost:3000/projects.xml

<?xml version="1.0" encoding="UTF-8"?> 2009-06-21T08:06:41-07:00 This is an awesome project. 3 Awesome 2009-06-21T08:06:41-07:00

Thanks Fred!

On Jun 21, 4:05 pm, Sarah A. [email protected]
wrote:

Frederick C. wrote:

I assume this is a typo and than in the real world you had remembered
the closing . Having done that you need to get curl to set
the content type appropriately.

I thought that posting to projects.xml would mean that setting the
content-type wasn’t required (even though I am doing that in cucumber,
just cuz it seems like the right thing to do)

posting to .foo means that rails thinks you want .foo returned to you
(so you’ll end up in your format.foo block if you’re using
respond_to )

The content type of the data you are posting is a separate thing
(although it would probably be sensible if it did assume that posting
to .xml means that you’re sending xml)

Fred

Thanks for your help, Fred.

For future reference, I’ve written up a little cheat sheet with the
various curl commands to access the various default Rails XML APIs:

http://www.ultrasaurus.com/sarahblog/2009/06/simple-web-services-with-rails/