# Mechanize and file\_column

**URL:** <https://www.ruby-forum.com/t/mechanize-and-file-column/59404>\
**Category:** Rails\
**Created:** [May 3, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/mechanize-and-file-column/59404 "2006-05-03T19:00:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [May 3, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/mechanize-and-file-column/59404/1 "2006-05-03T19:00:18Z")

</div>

Hi all,

Has anyone successfully used WWW::Mechanize with file\_column? I’m  
trying to upload a bunch of images to a custom controller, and  
something’s going wrong with the parameter names.

Using a noddy controller :create action that looks like this:

def create  
render :text =\> params.inspect  
end

I get this result from a manual upload from a form:

{“commit”=\>“Create”, “snapshot”=\>{“screenshot”=\>#\<File:/tmp/CGI8585.3\>,  
“screenshot\_temp”=\>"", “processes”=\>“foo bar”}, “action”=\>“create”,  
“controller”=\>“snapshots”}

(model name is Snapshot which has a file\_column :screenshot, controller  
is :snapshots)

But I get this result from Mechanize:

{“snapshot%5Bscreenshot%5D”=\>#\<File:/tmp/CGI8585.4\>, “action”=\>“create”,  
“controller”=\>“snapshots”, “snapshot%5Bprocesses%5D”=\>"",  
“snapshot%5Bscreenshot\_temp%5D”=\>""}

It looks to my untutored eye like the query keys are getting  
double-quoted on upload. Anyone else seen this, or anything like it?  
The code driving mechanize looks like this:

img = @form.file\_uploads.find{|f| f.name == ‘snapshot[screenshot]’}  
img\_name = time + ‘.png’  
img.file\_data = File.read(File.join(dirname, img\_name))  
img.file\_name = img\_name  
img.mime\_type = ‘image/png’  
@form.fields.find{|f| f.name == ‘snapshot[processes]’}.value = ‘’  
response = @agent.submit(@form)  
puts response.body

@form is found from @agent.get(blah).forms.first, @agent is a  
WWW::Mechanize.new.

I’m stumped - I can’t see anything that looks wrong here.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [May 3, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/mechanize-and-file-column/59404/2 "2006-05-03T19:08:08Z")

</div>

Alex Y. wrote:

> Hi all,  
>   
> I get this result from a manual upload from a form:
> 
> {“commit”=\>“Create”, “snapshot”=\>{“screenshot”=\>#\<File:/tmp/CGI8585.3\>,  
> “screenshot\_temp”=\>"", “processes”=\>“foo bar”}, “action”=\>“create”,  
> “controller”=\>“snapshots”}

\> \> But I get this result from Mechanize: \> \> {"snapshot%5Bscreenshot%5D"=\>#, "action"=\>"create", \> "controller"=\>"snapshots", "snapshot%5Bprocesses%5D"=\>"", \> "snapshot%5Bscreenshot\_temp%5D"=\>""}

I’m replying to my own post here, because I’ve done a little more  
investigation.

WWW::Mechanize uses WEBrick’s HTTPUtils.escape\_form on the uploaded  
form, which escapes ‘[’ and ‘]’ via the ‘unwise’ character collection.

Rails builds its deep params hash by splitting the query key strings on  
literal ‘[’ characters, without unquoting, which means that Mechanize’s  
submission isn’t interpreted in the intended way.

Now, which is correct? Firefox submits with literal ‘[’ characters,  
which makes me think that Rails is at least partially correct, but  
should it also accept ‘%5B…%5D’? Is WWW::Mechanize out of spec here?

Neither would be particularly tricky to patch, and because it’s blocking  
me I’ll probably do it over the next 24 hours either way. It’s just a  
question of which is in the wrong and needs correcting.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [May 3, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/mechanize-and-file-column/59404/3 "2006-05-03T19:14:18Z")

</div>

Alex Y. wrote:

> I’m replying to my own post here, because I’ve done a little more  
> investigation.  
> Further details: PHP doesn’t unquote the key values. That’s a fair  
> amount of evidence pointing to WWW::Mechanize being at fault…
