• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

HTTP, LWP POSTing

I need to be able to get a form using LWP and POST.    Following the pod, I've got this far, but the script won't compile, complains about Bad Header Type for Content adn Content_Type

 my $ua = LWP::UserAgent->new;
    my $req = HTTP::Request->new(POST => $url,
             Content_Type => 'form-data'                         Content      => [ name  => 'Gisle Aas',
            email => 'gisle@aas.no',
              gender => 'M',
            born   => '1964',
                                     ] );

# content straight from the pod there :)

Error messsage is:
 Carp::croak('Bad header argument') called at lib/HTTP/Message.pm line 52


Any ideas, anyone??

thanks

dave
0
davet666
Asked:
davet666
  • 4
  • 3
1 Solution
 
prakashk021799Commented:
# There should be a comma after 'form-data'.
# Is that a typo?

my $req = HTTP::Request->new(POST => $url,
    Content_Type => 'form-data',
    Content      => [ name  => 'Gisle Aas',
                               email => 'gisle@aas.no',
                               gender => 'M',
                               born   => '1964',
                             ] );
0
 
prakashk021799Commented:
What is line 52 of your script?
0
 
prakashk021799Commented:
Never mind. The error was talking about line 52 in lib/HTTP/Message.pm
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
davet666Author Commented:
I might actually have got it working (not entirely sure yet).  I gave up on the way the perldoc was suggesting, and am doing it a way I found on anotehr site:

my $ua = new LWP::UserAgent;
    $ua->agent("AgentName/0.1 " . $ua->agent);

    my $req = new HTTP::Request(POST => $url);
    $req->content_type('form-data');
    $req->content($param_string);

    my $res = $ua->request($req);

I'm having more luck with this, but I'm having to build $param_string (param1=val&param2=val2), rather than specifying an array, do you know of a way around this?

0
 
prakashk021799Commented:
Construct your parameter array like this:

$params = [
    'param1' => val1,
    'param1' => val2,
#  ..... etc.
];

# and then

$req->content($params);
0
 
davet666Author Commented:
Hey hey, my life is no longer a pit of despair :)  Thank you very much
0
 
davet666Author Commented:
all sorted! thanks
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now