develooper Front page | perl.libwww | Postings from May 2015

Is default Content-Type changed in a POST request?

Thread Next
From:
Octavian Rasnita
Date:
May 25, 2015 17:46
Subject:
Is default Content-Type changed in a POST request?
Message ID:
03218F3B33B948FBB6A8BF86436718A6@octavianf303f0
Hello,

I set 2 default headers using default_header() method and one of them is 
"Content-Type" which is set to "application/json", but it looks that it is 
overidden with "application/x-www-form-urlencoded" when doing a POST 
request. Is this intentionally?

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->default_header( "Content-Type" => "application/json" );
$ua->default_header( Authorization => "Bearer TOKEN_STRING_HERE" );

my $res = $ua->post( "https://api.digitalocean.com/v2/domains", Content => 
'json here' );
print $res->request->as_string;

The result is:
POST https://api.digitalocean.com/v2/domains
Authorization: Bearer TOKEN_STRING_HERE
User-Agent: libwww-perl/6.13
Content-Length: 9
Content-Type: application/x-www-form-urlencoded

json here


If I set this header in the POST request then it is kept and it is used as 
it should:

my $res = $ua->post(
    "https://api.digitalocean.com/v2/domains",
    content_type => 'application/json',
    Content => 'json here',
);

print $res->request->as_string;

The result is:
POST https://api.digitalocean.com/v2/domains
Authorization: Bearer TOKEN_STRING_HERE
User-Agent: libwww-perl/6.13
Content-Length: 9
Content-Type: application/json

json here


If I define the Content-Type header with default_header() as in the first 
example but do a GET request, then it is sent fine, it is not overidden.
Is there a way of defining the Content-Type header as default somewhere 
without needing to specify it in each POST request?

I use Perl 5.14, and LWP 6.13.

--Octavian


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About