retrieve file after posting some data

C

cerr

Hi There,

I'm using libwww to post data to a form. Once data is posted, the
server would send me back a file. How can i retrieve and safe this
file?
I'm posting the data with below code:

$response = $browser->get($url);
print $response->header('WWW-Authenticate'),"\n";
die "Error at $url\n ", $response->status_line, "\n Aborting"
unless $response->is_success;
#print "Authentication successful! Got ",$response->content_type, "
document!\n";
# Create a post request
my $post = HTTP::Request->new(POST => $PostPage);
#print "Posting data to \"$PostPage\"\n";
$post->content_type('application/x-www-form-urlencoded');
$post->content('export_profile=Current%20Profile');
# Pass request to the user agent and get a response back
my $res = $browser->request($post);

# Check the outcome of the response
if ($res->is_success) {
print $res->content , "\n";
}
else {
print $res->status_line, "\n";
}

How can I wait here until i get a file?
Thanks for hints and suggestions!
 
C

cerr

Quoth cerr <[email protected]>:











The file returned from the server should be in the $res object. See the
documentation for HTTP::Response.
Hi Ben,

Yep, been looking at that too but I haven't really gotten any further
unfortunately :( $res->filename e.g. is returning the filename of the
script i'm posting to but not the filename the server is sending back.
I don't know how i'd get the data. Been playing with
decoded_content('none') as well (cause the file coming back is in
binary) - but no success either - anymore hints maybe?

Thanks!
 
C

cerr

The data is in ->content. If ->filename is returning the request URI,
then probably the server is not sending a Content-Disposition header
that specifies a filename. Can you post $res->headers->as_string?
For me it seems like content only returns the html of the page i just
submitted data from.
res->headers->as_string contains HTTP::Response=HASH(0x9e368b0)-
headers->as_string...

hm, the html of the little form i would like to submit from looks like
this:
<form method="post" action="/cgi-bin/config.pl" enctype="application/x-
www-form-urlencoded" name="save">
<select name="export_profile">
<option selected="selected" value="Current Profile">Current Profile</
option>
<option value="Last Known Good Profile">Last Known Good Profile</
option>
<option value="Minimal Profile">Minimal Profile</option>
</select><input type="submit" name="save" value="Export" /></form>
and the POST string i assembled looks like:"export_profile=Current
%20Profile". I'm not sure if that's correct :( I don't know how i can
implement the form name "save" into my post string, do i need to do
that too?
Thanks!
 
C

C.DeRykus

For me it seems like content only returns the html of the page i just
submitted data from.
res->headers->as_string contains HTTP::Response=HASH(0x9e368b0)-


hm, the html of the little form i would like to submit from looks like
this:
<form method="post" action="/cgi-bin/config.pl" enctype="application/x-
www-form-urlencoded" name="save">
<select name="export_profile">
<option selected="selected" value="Current Profile">Current Profile</
option>
<option value="Last Known Good Profile">Last Known Good Profile</
option>
<option value="Minimal Profile">Minimal Profile</option>
</select><input type="submit" name="save" value="Export" /></form>
and the POST string i assembled looks like:"export_profile=Current
%20Profile". I'm not sure if that's correct :( I don't know how i can
implement the form name "save" into my post string, do i need to do
that too?

Does the backend config.pl print the Content-Disposition header that
was suggested..?

See "Easy naming and downloading of files":

http://savage.net.au/Perl-tutorials.html#tut_40
 
C

cerr

Quoth cerr <[email protected]>:







Right... What are you expecting to happen? Is there something else going
on here? Some JaveScript or something on the page that's interfering
with the normal form submission process? Are you able to usehttp://www2.research.att.com/sw/tools/wsp/with your normal browser to
get a better idea of what's actually going on?


No, that's not right. I'm going to guess you used

    warn "headers: $res->headers->as_string";

or something equivalent; that won't work since method calls aren't
interpolated. Try

    warn "headers: " . $res->headers->as_string;

Oh Yeah, that looks different:
headers: Connection:
close
Date: Wed, 04 Jan 2006 21:19:34
GMT
Server:
Apache
Content-Type: text/html; charset=ISO-8859-1
Client-Date: Tue, 23 Feb 2010 16:26:55 GMT
Client-Peer: 192.168.167.166:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=US/ST=California/L=Sunnyvale/O=Tropos
Networks/OU=Manufacturing/CN=Tropos Router/
[email protected]
Client-SSL-Cert-Subject: /C=US/ST=California/L=Sunnyvale/O=Tropos
Networks/OU=Manufacturing/CN=Tropos Router/
[email protected]
Client-SSL-Cipher: EDH-RSA-DES-CBC3-SHA
Client-SSL-Warning: Peer certificate not verified
Client-Transfer-Encoding: chunked
Link: <mailto:peter.sugiarto%40troposnetworks.com>; rev="made"
Title: Configuration Utility: INDOOR ROUTER
res->message: 200->code

But this doesn't really tell me anything either, does it?
What sort of 'filename' are you expecting?

Well I expected to see the filename that the browser would be
downloanding, that would be 'tropos.cfg'.
That looks correct for a POST from that form.


No. (Or, at any rate, not for a normal HTML form submission.)

Ok, then that should work fine with my post string
'export_profile=Current%20Profile'.
Thanks for your help!
 
C

cerr

Quoth cerr <[email protected]>:








It says the the server is *not* returning the configuration file at all,
but it sending back the same HTML form you started with.

I'm going to take a wild guess and suggest that maybe the server
requires you to accept cookies properly. Try using WWW::Mechanize (with
a properly-configured cookie store) to run through the whole
login/download process.



OK. If you install the LiveHTTPHeaders FF extension, you should see that
when you perform a successful download the server sends a
Content-Disposition header. HTTP::Response->filename will pick this up,
if it's there.

Ah, hold on, with this extension I figured out that the correct post
string is:'export_profile=Current+Profile&save=Export' and with that i
see binary data coming back in $res->content - now i just wrote this
in a binary file and it seems to work.

Thank you for your help!
 
C

C.DeRykus

Ah, hold on, with this extension I figured out that the correct post
string is:'export_profile=Current+Profile&save=Export' and with that i
see binary data coming back in $res->content - now i just wrote this
in a binary file and it seems to work.

off-topic but IMO the server should have returned a 400 'Bad Request'
if there's a missing query parameter. Even though covering a wide
range of possible error conditions, there's a clear signal that
the server encountered a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top