Jon
Jon

Reputation: 13

Set header fields for HTTP::Request::Common

I've problems to set header fields for sending requests by Perl modul 'HTTP::Request::Common'.

In subject to the corresponding server I have to set different header fields for my request.

So I want to use a sub 'MakeRequest()'

sub MakeRequest {
    my $url = shift;
    my $header = shift;
    my $content = shift;
    my $request = HTTP::Request::Common::POST($url, Header => $header, Content => $content);
    # I tried also my $request = HTTP::Request::Common::POST($url, $header, Content => $content);
    my $ua = LWP::UserAgent->new;
    my $response = $ua->request($request);
    return $response;
}

and pass some informations into it my $response = MakeRequest($url, GetRequestHeader(), $content);

sub GetRequestHeader {
    my $header = HTTP::Headers->new;
    $header->header('Content-Type' => 'application/json; charset=utf-8'); 
    $header->header('accept' => 'application/json'); 
    $header->authorization_basic($username, $password);
    return $header;

    # I tried this first, but got the same result as shown below
    #
    #   my %header = ( 
    #       'content_type' => 'application/json; charset=utf-8', 
    #       'authorization_basic' => ($username, $password), 
    #       'accept' =>  'application/json'
    #   );
    # return %header;

}

But all I got from the remote server is this

"Content type 'application/x-www-form-urlencoded' is not supported. Please use 'application/json; charset=utf-8'."

When I made a print Data::Dumper($request); I get

'_headers' => bless( {
                       'content-length' => 544,
                       'user-agent' => 'libwww-perl/6.15',
                       'header' => bless( {
                                            'content-type' => 'application/json; charset=utf-8',
                                            'authorization' => 'Basic Qxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==',
                                            'accept' => 'application/json'
                                          }, 'HTTP::Headers' ),
                       '::std_case' => {
                                         'header' => 'Header',
                                         'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
                                       },
                       'content-type' => 'application/x-www-form-urlencoded'
                     }, 'HTTP::Headers' ),

What's my mistake that the 'content-type' isn't overwritten by my header field settings?

Upvotes: 1

Views: 2631

Answers (1)

Laposhasú Acsa
Laposhasú Acsa

Reputation: 1580

According to the documentation:

HTTP::Request::Common::GET $url, Header => Value,...

is the same as

HTTP::Request->new(
   GET => $url,
   HTTP::Headers->new(Header => Value,...),
)

I think your original approach (the commented) is good, but you assign it the wrong way:

my $header = shift;
my $content = shift;
my $request = HTTP::Request::Common::POST($url, Header => $header, Content => $content);

Here, you create only one header, named Header. You can use the following if you have a HTTP::Headers object:

my $request = HTTP::Request::Common::POST($url, $header->flatten, Content => $content);

If you change GetRequestHeader to return a hash reference (as you have commented, but with return \%header instead of return %header), you can use the following:

my $request = HTTP::Request::Common::POST($url, %$hashref, Content => $content);

Upvotes: 2

Related Questions