Re: Is default Content-Type changed in a POST request?

[email protected] (Gisle Aas) Tue, 26 May 2015 22:05:13 +0200
Newsgroups perl.libwww
Message-ID <CAMx+QJ5=scbLccNO-VP9K6u8b48TPQN_G1MxHsTB+fhFHhA2Hw@mail.gmail.com>
--20cf303ea102f38bf1051701a4a1
Content-Type: text/plain; charset=UTF-8

On Mon, May 25, 2015 at 7:46 PM, Octavian Rasnita <[email protected]>
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
>
>

--20cf303ea102f38bf1051701a4a1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On M=
on, May 25, 2015 at 7:46 PM, Octavian Rasnita <span dir=3D"ltr">&lt;<a href=
=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>&gt;=
</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px=
 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-=
left-style:solid;padding-left:1ex">Hello,<br>
<br>
I set 2 default headers using default_header() method and one of them is &q=
uot;Content-Type&quot; which is set to &quot;application/json&quot;, but it=
 looks that it is overidden with &quot;application/x-www-form-urlencoded&qu=
ot; when doing a POST request. Is this intentionally?<br></blockquote><div>=
<br></div><div>It&#39;s a consequence of how HTTP::Request::Common::POST() =
currently works; $ua-&gt;post() is just a thin wrapper for that function.=
=C2=A0 I guess POST() could have been changed to not set the &quot;Content-=
Type&quot; if passed scalar &quot;Content&quot;.=C2=A0 It currently default=
s to &quot;application/x-www-form-urlencoded&quot; which cause the outer de=
fault to not apply.=C2=A0 Changing this has the potential to break other co=
de so it might be better to accept this behaviour.</div><div><br></div><div=
>Regards,</div><div>Gisle</div><div><br></div><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-co=
lor:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
use LWP::UserAgent;<br>
<br>
my $ua =3D LWP::UserAgent-&gt;new;<br>
$ua-&gt;default_header( &quot;Content-Type&quot; =3D&gt; &quot;application/=
json&quot; );<br>
$ua-&gt;default_header( Authorization =3D&gt; &quot;Bearer TOKEN_STRING_HER=
E&quot; );<br>
<br>
my $res =3D $ua-&gt;post( &quot;<a href=3D"https://api.digitalocean.com/v2/=
domains" target=3D"_blank">https://api.digitalocean.com/v2/domains</a>&quot=
;, Content =3D&gt; &#39;json here&#39; );<br>
print $res-&gt;request-&gt;as_string;<br>
<br>
The result is:<br>
POST <a href=3D"https://api.digitalocean.com/v2/domains" target=3D"_blank">=
https://api.digitalocean.com/v2/domains</a><br>
Authorization: Bearer TOKEN_STRING_HERE<br>
User-Agent: libwww-perl/6.13<br>
Content-Length: 9<br>
Content-Type: application/x-www-form-urlencoded<br>
<br>
json here<br>
<br>
<br>
If I set this header in the POST request then it is kept and it is used as =
it should:<br>
<br>
my $res =3D $ua-&gt;post(<br>
=C2=A0 =C2=A0&quot;<a href=3D"https://api.digitalocean.com/v2/domains" targ=
et=3D"_blank">https://api.digitalocean.com/v2/domains</a>&quot;,<br>
=C2=A0 =C2=A0content_type =3D&gt; &#39;application/json&#39;,<br>
=C2=A0 =C2=A0Content =3D&gt; &#39;json here&#39;,<br>
);<br>
<br>
print $res-&gt;request-&gt;as_string;<br>
<br>
The result is:<br>
POST <a href=3D"https://api.digitalocean.com/v2/domains" target=3D"_blank">=
https://api.digitalocean.com/v2/domains</a><br>
Authorization: Bearer TOKEN_STRING_HERE<br>
User-Agent: libwww-perl/6.13<br>
Content-Length: 9<br>
Content-Type: application/json<br>
<br>
json here<br>
<br>
<br>
If I define the Content-Type header with default_header() as in the first e=
xample but do a GET request, then it is sent fine, it is not overidden.<br>
Is there a way of defining the Content-Type header as default somewhere wit=
hout needing to specify it in each POST request?<br>
<br>
I use Perl 5.14, and LWP 6.13.<span class=3D""><font color=3D"#888888"><br>
<br>
--Octavian<br>
<br>
</font></span></blockquote></div><br></div></div>

--20cf303ea102f38bf1051701a4a1--