David Kahn
2010-Nov-18 19:32 UTC
Why does Rails try to parse XML received to a controller if mime type sent is "text/xml"
We have a web service to which clients post xml. One of our clients is using .NET and we receive their data, however if they send it as "text/xml" mime type, I receive it at the controller parsed like: {"WEB_REQUEST"=>{"AUTHENTICATION"=>{"LOGIN"=>"ft", "PASSWORD"=>"dir", "POSTING_SYSTEM"=>"zzzLending", "CLIENT_ID"=>"LDAP"}, "REQUEST"=>{"COMPARISON"=>{"ACTION"=>"new", "DOCUMENT"=>"<RESPONSE_GROUP MISMOVersionID=\"2.3.1\" _ID=\"RGRept000001\"> ... However if they disable the content type in their code -- removing this line: (//web.ContentType = "text/xml";) Then I get what I need and would expect: {"<WEB_REQUEST>\n\t<AUTHENTICATION>\n\t\t<POSTING_SYSTEM>zzzLending</POSTING_SYSTEM>\n\t\t<CLIENT_ID>LDAP</CLIENT_ID>\n\t\t<LOGIN>ft</LOGIN>\n\t\t<PASSWORD>dir</PASSWORD>\n\t</AUTHENTICATION>\n\t<REQUEST>\n\t\t<COMPARISON>\n\t\t\t<ACTION>new</ACTION>\n\t\t\t<DOCUMENT format"=>"\"MISMO\" type=\"credit report\"><![CDATA[<RESPONSE_GROUP MISMOVersionID=\"2.3.1\" _ID=\"RGRept000001\"> ..... Is there a way for me to handle within Rails the situation where they are posting the xml mime type? Really, I would expect them to be able to do so as it is in fact xml. Can I tell Rails to not do any magic to the incoming xml? (just kind of tacky to tell our client ''I know it is an xml request, but dont send it with the xml content type''). Thanks! David -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Nov-18 19:39 UTC
Re: Why does Rails try to parse XML received to a controller if mime type sent is "text/xml"
David Kahn wrote in post #962399:> We have a web service to which clients post xml. > > One of our clients is using .NET and we receive their data, however if > they > send it as "text/xml" mime type, I receive it at the controller parsed > like: > > {"WEB_REQUEST"=>{"AUTHENTICATION"=>{"LOGIN"=>"ft", "PASSWORD"=>"dir", > "POSTING_SYSTEM"=>"zzzLending", "CLIENT_ID"=>"LDAP"}, > "REQUEST"=>{"COMPARISON"=>{"ACTION"=>"new", "DOCUMENT"=>"<RESPONSE_GROUP > MISMOVersionID=\"2.3.1\" _ID=\"RGRept000001\"> ... > > However if they disable the content type in their code -- removing this > line: (//web.ContentType = "text/xml";) > > Then I get what I need and would expect: >{"<WEB_REQUEST>\n\t<AUTHENTICATION>\n\t\t<POSTING_SYSTEM>zzzLending</POSTING_SYSTEM>\n\t\t<CLIENT_ID>LDAP</CLIENT_ID>\n\t\t<LOGIN>ft</LOGIN>\n\t\t<PASSWORD>dir</PASSWORD>\n\t</AUTHENTICATION>\n\t<REQUEST>\n\t\t<COMPARISON>\n\t\t\t<ACTION>new</ACTION>\n\t\t\t<DOCUMENT> format"=>"\"MISMO\" type=\"credit report\"><![CDATA[<RESPONSE_GROUP > MISMOVersionID=\"2.3.1\" _ID=\"RGRept000001\"> .....Why are you trying to defeat Rails'' XML processing? It seems to me that it would normally be more efficient to work with it. [...]> Can I tell Rails to not do any magic to the > incoming > xml? (just kind of tacky to tell our client ''I know it is an xml > request, > but dont send it with the xml content type'').Probably. But I wonder if it''s really the right thing to do.> > Thanks! > > DavidBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Nov-18 19:47 UTC
Re: Re: Why does Rails try to parse XML received to a controller if mime type sent is "text/xml"
On Thu, Nov 18, 2010 at 1:39 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> David Kahn wrote in post #962399: > > We have a web service to which clients post xml. > > > > One of our clients is using .NET and we receive their data, however if > > they > > send it as "text/xml" mime type, I receive it at the controller parsed > > like: > > > > {"WEB_REQUEST"=>{"AUTHENTICATION"=>{"LOGIN"=>"ft", "PASSWORD"=>"dir", > > "POSTING_SYSTEM"=>"zzzLending", "CLIENT_ID"=>"LDAP"}, > > "REQUEST"=>{"COMPARISON"=>{"ACTION"=>"new", "DOCUMENT"=>"<RESPONSE_GROUP > > MISMOVersionID=\"2.3.1\" _ID=\"RGRept000001\"> ... > > > > However if they disable the content type in their code -- removing this > > line: (//web.ContentType = "text/xml";) > > > > Then I get what I need and would expect: > > > > {"<WEB_REQUEST>\n\t<AUTHENTICATION>\n\t\t<POSTING_SYSTEM>zzzLending</POSTING_SYSTEM>\n\t\t<CLIENT_ID>LDAP</CLIENT_ID>\n\t\t<LOGIN>ft</LOGIN>\n\t\t<PASSWORD>dir</PASSWORD>\n\t</AUTHENTICATION>\n\t<REQUEST>\n\t\t<COMPARISON>\n\t\t\t<ACTION>new</ACTION>\n\t\t\t<DOCUMENT > > format"=>"\"MISMO\" type=\"credit report\"><![CDATA[<RESPONSE_GROUP > > MISMOVersionID=\"2.3.1\" _ID=\"RGRept000001\"> ..... > > Why are you trying to defeat Rails'' XML processing? It seems to me that > it would normally be more efficient to work with it. >Well, if I realized this before I might have. However I have a parser built with Nokogiri which handles my requests very nicely and makes no sense to redo it as it is solid and well tested. I just noticed I can call params["WEB_REQUEST"].to_xml and I get "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <AUTHENTICATION>\n <LOGIN>xxx</LOGIN>\n <PASSWORD>xxx</PASSWORD>\n <POSTING-SYSTEM>xxx</POSTING-SYSTEM>\n <CLIENT-ID>xxx</CLIE So I *could* write a hack which (1) adds back the root <WEB_REQUEST> node, removes the <hash> node..... but this is real ugly. Do you have any idea where I would look to disable this the right way?> [...] > > Can I tell Rails to not do any magic to the > > incoming > > xml? (just kind of tacky to tell our client ''I know it is an xml > > request, > > but dont send it with the xml content type''). > > Probably. But I wonder if it''s really the right thing to do. > > > > > Thanks! > > > > David > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Biedenharn
2010-Nov-18 20:21 UTC
Re: Re: Why does Rails try to parse XML received to a controller if mime type sent is "text/xml"
On Nov 18, 2010, at 2:47 PM, David Kahn wrote:> On Thu, Nov 18, 2010 at 1:39 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsM1dTnIXmMWg@public.gmane.orgm > > wrote: > David Kahn wrote in post #962399: > > We have a web service to which clients post xml. > > > > One of our clients is using .NET and we receive their data, > however if > > they > > send it as "text/xml" mime type, I receive it at the controller > parsed > > like: > > > > {"WEB_REQUEST"=>{"AUTHENTICATION"=>{"LOGIN"=>"ft", > "PASSWORD"=>"dir", > > "POSTING_SYSTEM"=>"zzzLending", "CLIENT_ID"=>"LDAP"}, > > "REQUEST"=>{"COMPARISON"=>{"ACTION"=>"new", > "DOCUMENT"=>"<RESPONSE_GROUP > > MISMOVersionID=\"2.3.1\" _ID=\"RGRept000001\"> ... > > > > However if they disable the content type in their code -- removing > this > > line: (//web.ContentType = "text/xml";) > > > > Then I get what I need and would expect: > > > {"<WEB_REQUEST>\n\t<AUTHENTICATION>\n\t > \t<POSTING_SYSTEM>zzzLending</POSTING_SYSTEM>\n\t\t<CLIENT_ID>LDAP</ > CLIENT_ID>\n\t\t<LOGIN>ft</LOGIN>\n\t\t<PASSWORD>dir</PASSWORD>\n\t</ > AUTHENTICATION>\n\t<REQUEST>\n\t\t<COMPARISON>\n\t\t\t<ACTION>new</ > ACTION>\n\t\t\t<DOCUMENT > > format"=>"\"MISMO\" type=\"credit report\"><![CDATA[<RESPONSE_GROUP > > MISMOVersionID=\"2.3.1\" _ID=\"RGRept000001\"> ..... > > Why are you trying to defeat Rails'' XML processing? It seems to me > that > it would normally be more efficient to work with it. > > Well, if I realized this before I might have. However I have a > parser built with Nokogiri which handles my requests very nicely and > makes no sense to redo it as it is solid and well tested. > > I just noticed I can call > params["WEB_REQUEST"].to_xml > > and I get > > "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n > <AUTHENTICATION>\n <LOGIN>xxx</LOGIN>\n <PASSWORD>xxx</ > PASSWORD>\n <POSTING-SYSTEM>xxx</POSTING-SYSTEM>\n <CLIENT- > ID>xxx</CLIE > > So I *could* write a hack which (1) adds back the root <WEB_REQUEST> > node, removes the <hash> node..... but this is real ugly. Do you > have any idea where I would look to disable this the right way?This comment: Parsing YAML from a POST request is from two years ago and defines a config: ActionController::Base.param_parsers[Mime::YAML] = :yaml which sounds like it is in the same part of the processing that you''re interested in. I don''t know whether it is supported in whichever version of rails you''re using, but it certainly seems like a good path to investigate. -Rob> > > [...] > > Can I tell Rails to not do any magic to the > > incoming > > xml? (just kind of tacky to tell our client ''I know it is an xml > > request, > > but dont send it with the xml content type''). > > Probably. But I wonder if it''s really the right thing to do. > > > > > Thanks! > > > > David > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . > > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > .Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker
2010-Nov-19 15:22 UTC
Re: Why does Rails try to parse XML received to a controller if mime type sent is "text/xml"
The parameter parsing is happing in middleware. https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/params_parser.rb As you can see in the private method "parse_formatted_parameters(env)" the parsing of XML, JSON, YML, etc is based on mime-type. It seems to me like one could possibly write a small custom middleware that overrides the incoming mime-type and insert that middleware just before ActionController::ParamsParser. config.middleware.insert_before(existing_middleware, new_middleware, args) – Adds the new middleware before the specified existing middleware in the middleware stack. Seems like that would effectively disable the automatic parsing of incoming XML. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.