Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is default Content-Type changed in a POST request?

0 views
Skip to first unread message

Octavian Rasnita

unread,
May 25, 2015, 2:00:02 PM5/25/15
to lib...@perl.org
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

Gisle Aas

unread,
May 26, 2015, 4:15:04 PM5/26/15
to Octavian Rasnita, libwww
On Mon, May 25, 2015 at 7:46 PM, Octavian Rasnita <oras...@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

Octavian Rasnita

unread,
May 27, 2015, 1:00:02 AM5/27/15
to Gisle Aas, libwww
From: Gisle Aas

On Mon, May 25, 2015 at 7:46 PM, Octavian Rasnita <oras...@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

 
 
Thanks Gisle. So there are historical reasons.
I will create another wrapper in my code that sets that default Content-Type just once.
 
--Octavian
 
0 new messages