Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep Rails from Processing Large XML Post

In our rails application we have a many actions that do regular webapp actions. But, we have a single action that accepts a large XML file. I would like to keep rails from parsing the XML into params. Instead, I would like to be able to get the URL params ( /documents/{id}/action ) and then write out the xml file to a specific directory. How do I keep Rails from processing it?

How would I define the action to handle this?

def handle_xml
   # what to put here
end

The upload is done using Content-Type: application/xml It is a single file, and not part of a multipart form. The sample curl statement would be:

curl-H 'Accept: application/xml' -H 'Content-Type: application/xml' -X POST -d '<?xml version="1.0" encoding="UTF-8"?><test></test>' http://0.0.0.0:3000/controller/handle_xml
like image 628
christophercotton Avatar asked Oct 14 '25 09:10

christophercotton


2 Answers

If you want to prevent rails from automatically parsing the XML data into a hash of parameters, you'll have to replace the ParamsParser middleware with your own custom version.

When a file is posted to rails, the ParamsParser middleware modifies the request parameters and turns it into a Hash if the data format is xml. You can find the details in the params_parser.rb file in rails.

Here's a RoR mailing list message similar to the question that you've asked

Unfortunately, as a new user I can't post any more links, but you should search google with "Sanitizing POST params with custom Rack middleware" for some more details on writing custom rack middleware.

like image 114
adamc Avatar answered Oct 17 '25 01:10

adamc


I too have come across this problem recently. However mine is in an internal application where I have full control over both the Rails app and the clients connecting to it.

In my app the client POSTs a large XML data set to the Rails app. I wanted to process the XML document in a delayed job (resque). My workaround was to make the client use an alternate content-type. I used application/octet-stream. This prevents Rails from parsing the POST data. The data is available in request.raw_post.

like image 35
Tomek Avatar answered Oct 17 '25 02:10

Tomek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!