On Mon, May 25, 2015 at 7:46 PM, Octavian Rasnita <orasn...@gmail.com>
wrote:

> 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?
>

It's a consequence of how HTTP::Request::Common::POST() currently works;
$ua->post() is just a thin wrapper for that function.  I guess POST() could
have been changed to not set the "Content-Type" if passed scalar
"Content".  It currently defaults to "application/x-www-form-urlencoded"
which cause the outer default to not apply.  Changing this has the
potential to break other code so it might be better to accept this
behaviour.

Regards,
Gisle


> 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
>
>

Reply via email to