Home > Net >  Perl Curl PUT and Error 411 (Content Length)
Perl Curl PUT and Error 411 (Content Length)

Time:09-17

I am running Curl command on bash script and it is working perfectly, but the same command when I use in Perl is failing with the error below -

Error -

lb_update o/p: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>

Command -

curl -H "Content-Type:application/json" -H "Authorization: Bearer $authtoken" -X PUT "https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group>/providers/Microsoft.Network/loadBalancers/<load-balancer>?api-version=$apiversion" -d @output.json

Perl command -

$lb_update=`curl -H "Content-Type:application/json" -H "Authorization: Bearer $authtoken" -X PUT "https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group>/providers/Microsoft.Network/loadBalancers/<load-balancer>?api-version=$apiversion" -d @output.json';

I tried to modify the Perl command with the following parameter, but that did not help. -H "Content-Length:0" and --ignore-content-length

I am new to posting questions on these forums, please bear with me for any mistakes

CodePudding user response:

You should always add use strict and use warnings to your Perl scripts. @output inside your `...` is interpolated by Perl as an array. Since this array does not exist, @output is replaced by an empty string. This means that instead of doing -d @output.json as you'd like, your command is doing -d .json. To fix this issue, put a backslash before @output: -d \@output.json.

With strict and warnings enabled, the following errors/warnings would have been printed:

Possible unintended interpolation of @output in string at t.pl line 6.
Global symbol "@output" requires explicit package name (did you forget to declare "my @output"?) at t.pl line 6.

Moreover, instead of using system and curl, it would be more robust to use Perl modules. In particular, LWP::UserAgent is a great module for such thing. For instance:

my $ua = LWP::UserAgent->new;
my $req = $ua->put("https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group>/providers/Microsoft.Network/loadBalancers/<load-balancer>?api-version=$apiversion",
                   Content_Type  => 'application/json',
                   Authorization => "Bearer $authtoken",
                   Content => [ file => 'output.json' ]);
die $req->status_line unless $req->is_success;
my $content = $req->decoded_content;
  • Related