views:

56

answers:

1

I can't seem to find a good example of how to do this properly, the ones I have found aren't working for me.. I am trying to submit a form using perl mechanize, where the form has an image file, the form is as below, its actually a way I am trying to access this API for a website from which I have an account and using POST seems to be the easiest way to use their API:

<HTML>
<BODY>

<form 
 method="post" 
 name="image_upload"
 action="http://example-website.com" 
 enctype="multipart/form-data">
 <input type="hidden" name="field1"  value="">
 <input type="text"   name="username"  value="">
 <input type="text"   name="password"  value="">
 <input type="file"   name="pict">
 <input type="text"   name="field2"   value="0">
 <input type="text"   name="field3" value="0">
 <input type="submit" value="Send">
</form>

</BODY>
</HTML>

I have tried this, but its not uploading the image file, it seems.. any advice is appreciated. (I left out parts of the script for simplicity, but I am able to submit other forms using this method, just not ones that require a file upload)

fill form field:
$mech->form_name('image_upload');

$mech->set_fields( field1 => '', 
username => $username,
password => $password,
pict => '/home/user1/Desktop/pic.jpg',
field2 => '0',
field3 => '0'
);

#### submit form
$mech->submit();
A: 

For debugging, add autocheck => 1 to the $mech object, it will print the error and return on the earliest unsuccessful call.

Also, add a print $mech->content after the call to $mech->submit,

You may be getting some error page, in which case you can try setting user agent:

$mech->agent_alias( 'Windows IE 6' )

Or, maybe you need to get a cookie from an earlier page in order to upload. (you can test this by clearing cookies in your browser and trying to upload straight from the upload page)

miedwar
I don't need to debug it as I already know the unsuccessful call.. its the place where I am trying to upload the image.. its more about not being able to find the proper syntax for uploading an image in a form with mechanize than anything else
Rick
There's no special syntax for uploading an image. The code you pasted seems fine, could you elaborate on what 'unsuccessful' means? what happens after $mech->submit?
miedwar