tomPorter
2007-Jan-30 13:58 UTC
Newbie question about POSTing xml file to RESTful rails app
I have a newbie question about creating a new item using REST in rails 1.2. 1. I created my project: ''rails mra -d sqlite3'' 2. I generate all I can: ''cd mra'' ''script/generate scaffold_resource widget name:string description:string'' 3. I create database stuff: ''rake db:migrate'' 4. I now have a basic app: ''script/server'' I create a couple of widgets using the basic scaffolding forms. 5. I browse ''http://localhost:3000/widgets.xml'' I get a nice xml file out: <?xml version="1.0" encoding="UTF-8" ?> <widgets> <widget> <description>baz-ness</description> <id type="integer">1</id> <name>baz</name> </widget> <widget> <description>foo-ness</description> <id type="integer">2</id> <name>foo</name> </widget> </widgets> 6. I can use curl to POST a new widget to the app: ''curl -d "widget[name]=bar" -d "widget[description]=bar-ness" \ http://localhost:3000/widgets'' With a default app generated this way, is there a way to use curl to POST an XML file and generate a new widget? I do not think the ''create'' method in widgets_controller.rb distinguishes between the two URL''s used for a POST (/widgets and /widgets.xml) as far as how input is handled, and I am not sure how params[:widget] is populated (other than knowing that it is not populated correctly when I try something like this: ''curl -X POST -d \ "<widget><name>foobar</name><description>foobar-ness</description></ widget>" \ http://localhost:3000/widgets'' If I need to add code to parse the input XML file, where is the best place to code this, as a helper method? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick Olson
2007-Jan-30 15:01 UTC
Re: Newbie question about POSTing xml file to RESTful rails app
On 1/30/07, tomPorter <Tom.X.Porter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have a newbie question about creating a new item using REST in rails > 1.2. > > 1. I created my project: > ''rails mra -d sqlite3'' > > 2. I generate all I can: > ''cd mra'' > ''script/generate scaffold_resource widget name:string > description:string'' > > 3. I create database stuff: > ''rake db:migrate'' > > 4. I now have a basic app: > ''script/server'' > > I create a couple of widgets using the basic scaffolding forms. > > 5. I browse ''http://localhost:3000/widgets.xml'' I get a nice xml file > out: > <?xml version="1.0" encoding="UTF-8" ?> > <widgets> > <widget> > <description>baz-ness</description> > <id type="integer">1</id> > <name>baz</name> > </widget> > <widget> > <description>foo-ness</description> > <id type="integer">2</id> > <name>foo</name> > </widget> > </widgets> > > 6. I can use curl to POST a new widget to the app: > ''curl -d "widget[name]=bar" -d "widget[description]=bar-ness" \ > http://localhost:3000/widgets'' > > With a default app generated this way, is there a way to use curl to > POST an XML file and generate a new widget? I do not think the > ''create'' method in widgets_controller.rb distinguishes between the two > URL''s used for a POST (/widgets and /widgets.xml) as far as how input > is handled, and I am not sure how params[:widget] is populated (other > than knowing that it is not populated correctly when I try something > like this: > ''curl -X POST -d \ > "<widget><name>foobar</name><description>foobar-ness</description></ > widget>" \ > http://localhost:3000/widgets'' > > If I need to add code to parse the input XML file, where is the best > place to code this, as a helper method?No code needed, check out Beast for a simple, working example. http://weblog.techno-weenie.net/2006/12/12/taking-ares-out-for-a-test-drive map.resources automatically recognizes urls with formats (extensions like xml and js by default). XML has a default param parser that uses the xmlsimple lib to parse that xml into a hash in params[:widget]. Your controller code should stay the same then. If you have custom formats (atom, svn xml, json, whatever), you''ll have to write your own param parser. -- Rick Olson http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Zack Chandler
2007-Jan-30 16:14 UTC
Re: Newbie question about POSTing xml file to RESTful rails app
On 1/30/07, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 1/30/07, tomPorter <Tom.X.Porter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I have a newbie question about creating a new item using REST in rails > > 1.2. > > > > 1. I created my project: > > ''rails mra -d sqlite3'' > > > > 2. I generate all I can: > > ''cd mra'' > > ''script/generate scaffold_resource widget name:string > > description:string'' > > > > 3. I create database stuff: > > ''rake db:migrate'' > > > > 4. I now have a basic app: > > ''script/server'' > > > > I create a couple of widgets using the basic scaffolding forms. > > > > 5. I browse ''http://localhost:3000/widgets.xml'' I get a nice xml file > > out: > > <?xml version="1.0" encoding="UTF-8" ?> > > <widgets> > > <widget> > > <description>baz-ness</description> > > <id type="integer">1</id> > > <name>baz</name> > > </widget> > > <widget> > > <description>foo-ness</description> > > <id type="integer">2</id> > > <name>foo</name> > > </widget> > > </widgets> > > > > 6. I can use curl to POST a new widget to the app: > > ''curl -d "widget[name]=bar" -d "widget[description]=bar-ness" \ > > http://localhost:3000/widgets'' > > > > With a default app generated this way, is there a way to use curl to > > POST an XML file and generate a new widget? I do not think the > > ''create'' method in widgets_controller.rb distinguishes between the two > > URL''s used for a POST (/widgets and /widgets.xml) as far as how input > > is handled, and I am not sure how params[:widget] is populated (other > > than knowing that it is not populated correctly when I try something > > like this: > > ''curl -X POST -d \ > > "<widget><name>foobar</name><description>foobar-ness</description></ > > widget>" \ > > http://localhost:3000/widgets'' > > > > If I need to add code to parse the input XML file, where is the best > > place to code this, as a helper method? > > No code needed, check out Beast for a simple, working example. > > http://weblog.techno-weenie.net/2006/12/12/taking-ares-out-for-a-test-drive > > map.resources automatically recognizes urls with formats (extensions > like xml and js by default). XML has a default param parser that uses > the xmlsimple lib to parse that xml into a hash in params[:widget]. > Your controller code should stay the same then. If you have custom > formats (atom, svn xml, json, whatever), you''ll have to write your own > param parser. > > > -- > Rick Olson > http://weblog.techno-weenie.net > http://mephistoblog.com > > > >You also might use the filename notation with curl to avoid having to type the xml message on the command line. Just create a file new_widget.xml and then send it using curl: curl -H "Content-Type: text/xml" -d @new_widget.xml -X POST http://localhost:3000/widgets -- Zack Chandler http://depixelate.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
tomPorter
2007-Jan-30 20:11 UTC
Re: Newbie question about POSTing xml file to RESTful rails app
On Jan 30, 11:14 am, "Zack Chandler" <zackchand...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You also might use the filename notation with curl to avoid having to > type the xml message on the command line. Just create a file > new_widget.xml and then send it using curl: > > curl -H "Content-Type: text/xml" -d @new_widget.xml -X POSThttp://localhost:3000/widgets > > -- > Zack Chandlerhttp://depixelate.com- Hide quoted text -Thanks to both Zack and Rick. I found that I was not indicating that my Content-Type as ''text/xml'' in my curl command, and that is what is needed to trigger the XML parsing. Pretty cool stuff! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick Olson
2007-Jan-30 20:20 UTC
Re: Newbie question about POSTing xml file to RESTful rails app
> Thanks to both Zack and Rick. > > I found that I was not indicating that my Content-Type as ''text/xml'' > in my curl command, and that is what is needed to trigger the XML > parsing. > > Pretty cool stuff!Just adding .xml to the filename works too. You don''t have to specificially set content-type or accepts AFAIK. -- Rick Olson http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
tomPorter
2007-Jan-31 01:09 UTC
Re: Newbie question about POSTing xml file to RESTful rails app
I will have to experiment with just submitting an XML file, but i _thought_ when I tried this w/o the Content-Type header ,it created a null widget: ID only blank attributes. On Jan 30, 3:20 pm, "Rick Olson" <technowee...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Just adding .xml to the filename works too. You don''t have to > specificially set content-type or accepts AFAIK. > > -- > Rick Olsonhttp://weblog.techno-weenie.nethttp://mephistoblog.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---