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 ''<project><title>Awesome</title><description>This is an awesome project.</description>'' http://localhost:3000/projects.xml <?xml version="1.0" encoding="UTF-8"?> <project> <created-at type="datetime">2009-06-21T14:09:08Z</created-at> <description nil="true"></description> <id type="integer">1</id> <title nil="true"></title> <updated-at type="datetime">2009-06-21T14:09:08Z</updated-at> </project> The log says: Processing ProjectsController#create to xml (for 127.0.0.1 at 2009-06-21 07:09:08) [POST] Parameters: {"<project><title>Awesome</title><description>This is an awesome project.</description>"=>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 -- Posted via http://www.ruby-forum.com/.
Sarah Allen 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 -- Posted via http://www.ruby-forum.com/.
On Jun 21, 3:26 pm, Sarah Allen <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 ''<project><title>Awesome</title><description>This is > an awesome project.</description>''http://localhost:3000/projects.xml >I assume this is a typo and than in the real world you had remembered the closing </project>. Having done that you need to get curl to set the content type appropriately. Fred> <?xml version="1.0" encoding="UTF-8"?> > <project> > <created-at type="datetime">2009-06-21T14:09:08Z</created-at> > <description nil="true"></description> > <id type="integer">1</id> > <title nil="true"></title> > <updated-at type="datetime">2009-06-21T14:09:08Z</updated-at> > </project> > > The log says: > Processing ProjectsController#create to xml (for 127.0.0.1 at 2009-06-21 > 07:09:08) [POST] > Parameters: {"<project><title>Awesome</title><description>This is an > awesome project.</description>"=>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 > -- > Posted viahttp://www.ruby-forum.com/.
Frederick Cheung wrote:> I assume this is a typo and than in the real world you had remembered > the closing </project>. 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 -- Posted via http://www.ruby-forum.com/.
Frederick Cheung wrote:> I assume this is a typo and than in the real world you had remembered > the closing </project>. 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 ''<project><title>Awesome</title><description>This is an awesome project.</description></project>'' -H "Content-Type: application/xml" http://localhost:3000/projects.xml <?xml version="1.0" encoding="UTF-8"?> <project> <created-at type="datetime">2009-06-21T08:06:41-07:00</created-at> <description>This is an awesome project.</description> <id type="integer">3</id> <title>Awesome</title> <updated-at type="datetime">2009-06-21T08:06:41-07:00</updated-at> </project> Thanks Fred! -- Posted via http://www.ruby-forum.com/.
On Jun 21, 4:05 pm, Sarah Allen <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > > I assume this is a typo and than in the real world you had remembered > > the closing </project>. 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> Sarah > -- > Posted viahttp://www.ruby-forum.com/.
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/ -- Posted via http://www.ruby-forum.com/.