retrieve webpage with POST

D

Dean Banko

Hello!

Does anyone know how to retreive a page generated as form request?
I need to post some data like in the row:
my $req=HTTP::Request->new(POST=>$url);
but there are also some parameters that I must put into POST, but I don't
know the syntax :-(
I know that must be something like:
my $req=HTTP::Request->new(POST=>$url, [field=>'1', valid=>'yes');



$url=[URL]http://www.somelink.com[/URL]"
my $ua=LWP::UserAgent->new();
my $req=HTTP::Request->new(POST=>$url);
my $response=$ua->request($req);
if ($response->is_error()){$response->error_as_HTML()}
$content=$response->content();

Can anyone help me?

Regards,
Dean
 
G

Gunnar Hjalmarsson

Dean said:
Does anyone know how to retreive a page generated as form request?
I need to post some data like in the row:
my $req=HTTP::Request->new(POST=>$url);
but there are also some parameters that I must put into POST, but I
don't know the syntax :-(
I know that must be something like:
my $req=HTTP::Request->new(POST=>$url, [field=>'1', valid=>'yes');[/QUOTE]

Assuming you are talking about two fields named 'field' and 'valid',
and whose values you want to be '1' respective 'yes', I'd try:

my $req=HTTP::Request->new(POST=>$url);
$req->content_type('application/x-www-form-urlencoded');
$req->content('field=1&valid=yes');

(untested)
 
T

Tad McClellan

Dean Banko said:
I know that must be something like:
my $req=HTTP::Request->new(POST=>$url, [field=>'1', valid=>'yes');[/QUOTE]


use HTTP::Request::Common;
$ua = LWP::UserAgent->new;
$ua->request(POST $url, [field => '1', valid => 'yes']);

Can anyone help me?


The documentation for HTTP::Request says to see also HTTP::Request::Common.

If you'd paid close attention while reading the docs for the module
that you are using, you wouldn't _need_ anyone else's help. :)
 
G

Gunnar Hjalmarsson

Tad said:
Dean Banko said:
I know that must be something like:
my $req=HTTP::Request->new(POST=>$url, [field=>'1', valid=>'yes');[/QUOTE]

use HTTP::Request::Common;
$ua = LWP::UserAgent->new;
$ua->request(POST $url, [field => '1', valid => 'yes']);
Can anyone help me?

The documentation for HTTP::Request says to see also
HTTP::Request::Common.

Does it? I for one am not able to find that reference.

Anyway, thanks for the tip. I suppose it results in the same HTTP
request as my suggestion would do.
If you'd paid close attention while reading the docs for the module
that you are using, you wouldn't _need_ anyone else's help. :)

Hrrrm...
 
M

Matt Garrish

Gunnar Hjalmarsson said:
Does it? I for one am not able to find that reference.

Umm, it's right in the See Also section of the documentation. A quick skim
through Poe's The Purloined Letter might be in order... ; )

Matt
 
G

Gunnar Hjalmarsson

Tad said:

There it is, obviously... Thanks!

To *not* find it, I searched CPAN for HTTP::Request and ended up at
http://search.cpan.org/~rse/lcwa-1.0.0/lib/lwp/lib/HTTP/Request.pm
-----------------------------------------------------^^^^^^^^^^^^

I should probably have been more attentive, but...

http://search.cpan.org/~rse/lcwa-1.0.0/ seems to be a 'historical
archive' of past versions of quite a few modules. :(

Is that in accordance with the PAUSE rules?
 
D

Dean Banko

Hello Gunnar!

I thank you a lot for your assistance!
The syntax that you offered is functional, I've tested it and it works!

Thank you very much!
Happy New Year,
Dean

Gunnar Hjalmarsson said:
Dean said:
Does anyone know how to retreive a page generated as form request?
I need to post some data like in the row:
my $req=HTTP::Request->new(POST=>$url);
but there are also some parameters that I must put into POST, but I
don't know the syntax :-(
I know that must be something like:
my $req=HTTP::Request->new(POST=>$url, [field=>'1', valid=>'yes');[/QUOTE]

Assuming you are talking about two fields named 'field' and 'valid',
and whose values you want to be '1' respective 'yes', I'd try:

my $req=HTTP::Request->new(POST=>$url);
$req->content_type('application/x-www-form-urlencoded');
$req->content('field=1&valid=yes');

(untested)
 
D

Dean Banko

Hello Gunnar and Tad!

Thank you both for your assistance!
I've read all refferences you gave, but nowhere is written so clear as
Gunnar posted as the untested version to my original posting.

The thing works now correctly and appreciate your help, both!

I wish Happy New Year to you and your families,
many success and health
Dean Banko, Croatia
 
G

Gunnar Hjalmarsson

[ Please do not top post! ]

Dean said:
I thank you a lot for your assistance!
The syntax that you offered is functional, I've tested it and it
works!

I'm glad it works, Dean.

It should be noted that the field names and values in the example
consist of letters and digits only. If you can't tell that that's
always the case, I recommend that you take a look at
HTTM::Request::Common as Tad suggested. That module takes care of URI
escaping when needed. Otherwise, with my 'direct' method, you'd need
to handle the URI escaping separately.
 
P

Peter Scott

Dean said:
Does anyone know how to retreive a page generated as form request?
I need to post some data like in the row:
my $req=HTTP::Request->new(POST=>$url);
but there are also some parameters that I must put into POST, but I
don't know the syntax :-(
I know that must be something like:
my $req=HTTP::Request->new(POST=>$url, [field=>'1', valid=>'yes' );[/QUOTE]

Assuming you are talking about two fields named 'field' and 'valid',
and whose values you want to be '1' respective 'yes', I'd try:

my $req=HTTP::Request->new(POST=>$url);
$req->content_type('application/x-www-form-urlencoded');
$req->content('field=1&valid=yes');

This is unnecessarily fraught with danger. It will fail the first time
anyone tries to use values that ought to be URL escaped. And can you be
certain without checking the source whether the Content-length header
will be set correctly ? There is a clearly better alternative:

use HTTP::Request::Common;
my $req = POST $url, [ field=>'1', valid=>'yes' ];

The OP was almost there.
(untested)

No need to test.
 
G

Gunnar Hjalmarsson

Peter said:
This is unnecessarily fraught with danger.

Danger? In that case, the POD for LWP.pm is dangerous, because its
first example includes just those lines.
It will fail the first time anyone tries to use values that ought
to be URL escaped.

Yep, i.e. if you don't escape them first. I had already posted that
warning.
And can you be certain without checking the source whether the
Content-length header will be set correctly ?

Which source? I don't think it is set. Does it need to be? Again, see
LWP.pm.

I have at least noticed that the the remote server sets the
content-length environment variable.
There is a clearly better alternative:

use HTTP::Request::Common;
my $req = POST $url, [ field=>'1', valid=>'yes' ];

Yes, it seems to be a more convenient way that I wasn't aware of
before Tad mentioned it.
 
P

Peter Scott

Danger? In that case, the POD for LWP.pm is dangerous, because its
first example includes just those lines.

So it does. Perusal of Backpan reveals that those lines precede
the introduction of HTTP::Request::Common. I shall submit a patch.
Thank you for pointing them out.
Which source? I don't think it is set. Does it need to be?

That's my point; you're left wondering.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top