Perl LWP Unauthorized While Curl Ok - perl

I am attempting to recreate a working CURL command with LWP in Perl and I'm getting a 401 unauthorized error from LWP. The command posts JSON to a specific URL as seen in the code below. The server FQDN, IP, port and path are correct and identical between the curl and Perl as are the credentials and realm. Any help would be appreciated - thank you!
Below is the working syntax in cURL and debug output:
#curl -v -k -u "USERNAME:PASSWORD" -X POST <SERVER_URL> -d '<JSON CONTENT>';
* About to connect() to <SERVER_URL> port 443 (#0)
* Trying <SERVER_IP>... connected
* Connected to <SERVER_URL> (<SERVER_IP>) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* warning: ignoring value of ssl.verifyhost
* skipping SSL peer certificate verification
* SSL connection using TLS_DHE_RSA_WITH_AES_256_CBC_SHA
* Server certificate:
* subject: [REDACTED]
* start date: Apr 21 00:00:00 2016 GMT
* expire date: Apr 21 23:59:59 2019 GMT
* common name: <SERVER_URL>
* issuer: [REDACTED]
* Server auth using Basic with user '<USERNAME>'
> POST <SERVER_PATH> HTTP/1.1
> Authorization: Basic <BASE64-ENCODED USERNAME:PASSWORD>
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: <SERVER_URL>
> Accept: */*
> Content-Length: 144
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
< Date: Fri, 13 May 2016 13:48:42 GMT
< Server: Apache
< Content-Type: application/json
< Content-Length: 256
<
* Connection #0 to host <SERVER_URL> left intact
* Closing connection #0
Updated Perl code and output per Steffen's suggestion. I corrected an initial quoting error and also added the Accept header to the LWP post command:
use strict;
use warnings;
use LWP::UserAgent;
use Data::Dumper;
my $server_root_with_port = "<FQDN>:443";
my $url = "<SERVER_URL>";
my $realm = "<SERVER_REALM>";
my $json = "<JSON CONTENT>";
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
$ua->credentials($server_root_with_port,$realm,$username=>$password);
$response = $ua->post($url, 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*', 'Content' => $json);
print Dumper $response;
exit;
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '',
'_rc' => '400',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Mon, 16 May 2016 14:18:59 GMT',
'client-ssl-cert-issuer' => '[REDACTED]',
'client-ssl-cipher' => 'AES128-SHA256',
'client-peer' => '<SERVER_IP>:443',
'content-length' => '0',
'::std_case' => {
'client-date' => 'Client-Date',
'client-response-num' => 'Client-Response-Num',
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject',
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer',
'client-ssl-cipher' => 'Client-SSL-Cipher',
'client-peer' => 'Client-Peer',
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
},
'client-date' => 'Mon, 16 May 2016 14:18:59 GMT',
'client-ssl-cert-subject' => '[REDACTED]',
'server' => 'Apache',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_previous' => bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn\'t understand how to supply
the credentials required.</p>
</body></html>
',
'_rc' => '401',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Mon, 16 May 2016 14:18:59 GMT',
'client-ssl-cert-issuer' => '[REDACTED]',
'client-ssl-cipher' => 'AES128-SHA256',
'client-peer' => '<SERVER_IP>:443',
'content-length' => '381',
'::std_case' => {
'client-date' => 'Client-Date',
'client-response-num' => 'Client-Response-Num',
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject',
'title' => 'Title',
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer',
'client-ssl-cipher' => 'Client-SSL-Cipher',
'client-peer' => 'Client-Peer',
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
},
'client-date' => 'Mon, 16 May 2016 14:18:59 GMT',
'content-type' => 'text/html; charset=iso-8859-1',
'client-ssl-cert-subject' => '[REDACTED]',
'www-authenticate' => 'Basic realm="<SERVER_REALM>"',
'title' => '401 Unauthorized',
'server' => 'Apache',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_msg' => 'Unauthorized',
'_request' => bless( {
'_content' => '<JSON_CONTENT>',
'_uri' => bless( do{\(my $o = '<SERVER_URL>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.15',
'content-type' => 'application/x-www-form-urlencoded',
'accept' => '*/*',
'content-length' => 144,
'::std_case' => {
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
}
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_previous'}{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' ),
'_msg' => 'Bad Request',
'_request' => bless( {
'_protocol' => undef,
'_content' => '<JSON_CONTENT>',
'_uri' => bless( do{\(my $o = '<SERVER_URL>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.15',
'content-type' => 'application/x-www-form-urlencoded',
'accept' => '*/*',
'content-length' => 144,
'authorization' => '<BASE64-ENCODED USERNAME:PASSWORD>',
'::std_case' => {
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
}
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );
Perl Revision #1:
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use Data::Dumper;
my $fqdn_port = "<FQDN>:443";
my $url = "<URL>";
my $realm = "<REALM>";
my $username = "<USERNAME>";
my $password = "<PASSWORD>";
my $json = "<JSON_CONTENT>";
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
#$ua->credentials($fqdn_port,$realm,$username=>$password);
#my $response = $ua->post($url, 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*', Content => $json);
my $request = HTTP::Request->new('POST',$url);
$request->header('Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*');
$request->authorization_basic($username,$password);
$request->content($json);
my $response = $ua->request($request);
print Dumper $response;
exit;
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '',
'_rc' => '400',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Mon, 16 May 2016 15:41:10 GMT',
'client-ssl-cert-issuer' => '[REDACTED]',
'client-ssl-cipher' => 'AES128-SHA256',
'client-peer' => '<SERVER_IP>:443',
'content-length' => '0',
'::std_case' => {
'client-date' => 'Client-Date',
'client-response-num' => 'Client-Response-Num',
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject',
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer',
'client-ssl-cipher' => 'Client-SSL-Cipher',
'client-peer' => 'Client-Peer',
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
},
'client-date' => 'Mon, 16 May 2016 15:41:10 GMT',
'client-ssl-cert-subject' => '[REDACTED]',
'server' => 'Apache',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_msg' => 'Bad Request',
'_request' => bless( {
'_content' => '<JSON_CONTENT>',
'_uri' => bless( do{\(my $o = '<URL>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.15',
'content-type' => 'application/x-www-form-urlencoded',
'accept' => '*/*',
'::std_case' => {
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
},
'authorization' => 'Basic <BASE64-ENCODED USERNAME:PASSWORD>'
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );

TL;TR: always use strict !!
$response = $ua->post($url, Content-Type => 'application/json', Content => $json);
You've missed to quote around Content-Type which would have been detected by use strict. This result of this is a strange header 0 you see in the debug output:
'content-type' => 'application/x-www-form-urlencoded',
'0' => 'application/json',
'content-length' => 144,
And this also means that the setting of the content-type is wrong. This together results in the server not accepting your request:
'_rc' => '400',
...
'_msg' => 'Bad Request',
To understand what happens here look at what Perl actually sees in such code:
$ perl -MO=Deparse -e 'my %x = (Content-Type => 1, Foo => 2 )'
my(%x) = ('Content' - 'Type', 1, 'Foo', 2);
This shows that it will interpret the unquoted Content-Type as 'Content' - 'Type'. And since subtraction is not defined for strings they will be cast to an integer, i.e. 0. Which means the result is 0 (0-0).
When using strict you get instead:
perl -Mstrict -e 'my %x = (Content-Type => 1, Foo => 2 )'
Bareword "Content" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.

Found a solution - perhaps not the most elegant, but it does the trick. Ended up using HTTP::Request::Common to get around the authorization issue and reversed the quoting on the JSON variable to mitigate the 400 Bad Request and success - getting a correct return from the server! Thanks for the help #steffen_ullrich.
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use Data::Dumper;
my $fqdn_port = "<FQDN>:443";
my $url = "<URL>";
my $realm = "<REALM>";
my $username = "<USERNAME>";
my $password = "<PASSWORD>";
my $json = '<JSON_CONTENT>';
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $request = HTTP::Request->new('POST',$url);
$request->header('Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*');
$request->authorization_basic($username,$password);
$request->content($json);
my $response = $ua->request($request);
print Dumper $response;
exit;
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '<RETURN JSON FROM SERVER>',
'_rc' => '200',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Mon, 16 May 2016 16:07:07 GMT',
'client-ssl-cert-issuer' => '[REDACTED]',
'client-ssl-cipher' => 'AES128-SHA256',
'client-peer' => '<SERVER_IP>:443',
'content-length' => '233',
'::std_case' => {
'client-date' => 'Client-Date',
'client-response-num' => 'Client-Response-Num',
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject',
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer',
'client-ssl-cipher' => 'Client-SSL-Cipher',
'client-peer' => 'Client-Peer',
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
},
'client-date' => 'Mon, 16 May 2016 16:07:07 GMT',
'content-type' => 'application/json',
'client-ssl-cert-subject' => '[REDACTED]',
'server' => 'Apache',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_msg' => 'OK',
'_request' => bless( {
'_content' => '<JSON_CONTENT>',
'_uri' => bless( do{\(my $o = '<URL>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.15',
'content-type' => 'application/x-www-form-urlencoded',
'accept' => '*/*',
'::std_case' => {
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
},
'authorization' => 'Basic <BASE64-ENCODED USERNAME:PASSWORD>'
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );

Related

Get responses from External API need Login session

I want to get data from External API. but, I must login to get session before Getting data and I must Logout after get data
this is my code
use GuzzleHttp\Client;
$client = new Client();
$res = $client->request('POST', 'Login_URL', [
'json' => [
"JSON param"
]
]);
$res = $client->request('GET', 'Get_URL');
$res = $client->request('POST', 'Logout_URL');
but I can only the first step (Login). and I getting error message in the second step to get data
Client error: `GET "Get_URL" ` resulted in a `401 Unauthorized` response:{"message":"You are not logged in."}
how I can run all this code with login session on first step ?
this code for you to do login and logout session
$cookie_jar = tempnam("tmp", "cookie");
//login
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER=> true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION=> true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_POSTFIELDS =>'{
"name" : "****",
"password" : "****",
"company" : "****"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'),
)
);
$response = curl_exec($curl);
//get data
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_COOKIEFILE => $cookie_jar
)
);
$datapo = json_decode(curl_exec($curl));
//logout
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"name" : "****",
"password" : "****",
"company" : "****"
}',
CURLOPT_COOKIEFILE => $cookie_jar
)
);
$response = curl_exec($curl);

Undefined offset: 0 with LARAVEL 5.6 and GuzzleHttp POST request

can you help me please, I want to use UBER API with GuzzleHttp
$uber_client = new Client([
'base_uri' => 'https://api.uber.com/v1.2/',
'timeout' => 2.0,
'headers' => [
'content_type' => 'application/json',
'Accept-Language' => 'fr_FR',
'Authorization' => "Bearer $uber_access_token"],
]);
to have Ride Request - Estimate :
$resp_uber = $uber_client->post("requests/estimate", [
'auth' => [
'Authorization' => "Bearer $uber_access_token"
],
'form_params' => [
'start_latitude' => $start_latitude,
'start_longitude' => $start_longitude,
'end_latitude' => $end_latitude,
'end_longitude' => $end_longitude,
]
]);
=> Erreur :
ErrorException (E_NOTICE)
Undefined offset: 0

Extracting data from a multi-level hash

I am getting the data structure below as a response from a web service call.
my $triggers1 = $zabbix->raw('trigger','get', $options1);
print Dumper($triggers1);
Output
$VAR1 = {
'10305122' => {
'hosts' => [
{
'name' => 'pc4b12cf254444',
'maintenance_type' => '0',
'hostid' => '19295'
}
],
'priority' => '1',
'status' => '0',
'dependencies' => [],
'templateid' => '9892568',
'comments' => '',
'state' => '0',
'triggerid' => '10305122',
'expression' => '{14127122}=0',
'error' => '',
'url' => '',
'flags' => '0',
'value' => '0',
}
324234' => {
'hosts' => [
{
'name' => 'pc45657ba34gy0423',
'maintenance_type' => '0',
'hostid' => '19439'
}
],
'priority' => '1',
'status' => '0',
'dependencies' => [],
'templateid' => '9896452',
'comments' => '',
'state' => '0',
'triggerid' => '10324234',
'expression' => '{14167689}=0',
'error' => '',
'url' => '',
'flags' => '0',
'value' => '0',
'value_flags' => '0',
'lastchange' => '1420266068',
'type' => '0'
};
etc
There are multiple similar records
From this output, I want to print the values of 'name' and 'value'.
How do I print this using Perl?
How about:
my $triggers1 = {
'10305122' => {
'hosts' => [
{
'name' => 'pc4b12cf254444',
'maintenance_type' => '0',
'hostid' => '19295'
}
],
'priority' => '1',
'status' => '0',
'dependencies' => [],
'templateid' => '9892568',
'comments' => '',
'state' => '0',
'triggerid' => '10305122',
'expression' => '{14127122}=0',
'error' => '',
'url' => '',
'flags' => '0',
'value' => '0',
},
324234 => {
'hosts' => [
{
'name' => 'pc45657ba34gy0423',
'maintenance_type' => '0',
'hostid' => '19439'
}
],
'priority' => '1',
'status' => '0',
'dependencies' => [],
'templateid' => '9896452',
'comments' => '',
'state' => '0',
'triggerid' => '10324234',
'expression' => '{14167689}=0',
'error' => '',
'url' => '',
'flags' => '0',
'value' => '0',
'value_flags' => '0',
'lastchange' => '1420266068',
'type' => '0'
}
};
Code disconnected from data for readability:
foreach my $k (keys %$triggers1) {
print "key=$k";
foreach my $h (#{$triggers1->{$k}{hosts}}) {
print "\nname=",$h->{name} // 'not defined';
// use this ^^ to avoid Use of uninitialized value in print at
}
print "\nvalue=",$triggers1->{$k}{value} // 'not defined',"\n";
}
Output:
key=324234
name=pc45657ba34gy0423
value=0
key=10305122
name=pc4b12cf254444
value=0

Calls to update a user return 200 OK but do not update user

Back story:
Migrating from the Provisioning API to the Admin SDK Directory API. Using Perl.
I can successfully obtain a Bearer token, and I can use the token to obtain an individual User Resource and User Resource listing of the entire domain. This all works fine. I have ensured that I'm using the proper scope in the token request (https://www.googleapis.com/auth/admin.directory.user).
Problem:
Calls to update a user are returning 200 OK (expected) but the changes are not being picked up.
Using LWP to PUT the update request. Here's a dump of the LWP object after the request comes back. You can see that I'm getting a 200 OK response and a User Resource object as part of the response. You can also see that the returned User Resource does not reflect the changes I've sent in the request. I have confirmed in the admin console for the domain that the changes were not picked up.
Any help would be appreciated.
'_content' => '{
"kind": "admin#directory#user",
"id": "somenumber",
"etag": "\\"etag\\"",
"primaryEmail": "user#googletestdomain",
"name": {
"givenName": "user",
"familyName": "name",
"fullName": "user name"
},
"isAdmin": false,
"isDelegatedAdmin": false,
"lastLoginTime": "2014-10-02T17:20:02.000Z",
"creationTime": "2010-01-04T22:27:44.000Z",
"agreedToTerms": true,
"suspended": false,
"changePasswordAtNextLogin": false,
"ipWhitelisted": false,
"emails": [
{
"address": "user#googletestdomain",
"primary": true
},
],
"customerId": "C01id",
"orgUnitPath": "/",
"isMailboxSetup": true,
"includeInGlobalAddressList": true
}
'
'_headers' => HTTP::Headers=HASH(0x2031048)
'::std_case' => HASH(0x2031240)
'alternate-protocol' => 'Alternate-Protocol'
'client-date' => 'Client-Date'
'client-peer' => 'Client-Peer'
'client-response-num' => 'Client-Response-Num'
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer'
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject'
'client-ssl-cipher' => 'Client-SSL-Cipher'
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
'x-content-type-options' => 'X-Content-Type-Options'
'x-frame-options' => 'X-Frame-Options'
'x-xss-protection' => 'X-XSS-Protection'
'alternate-protocol' => '443:quic,p=0.01'
'cache-control' => 'no-cache, no-store, max-age=0, must-revalidate'
'client-date' => 'Mon, 27 Oct 2014 17:48:14 GMT'
'client-peer' => '173.194.79.95:443'
'client-response-num' => 1
'client-ssl-cert-issuer' => '/C=US/O=Google Inc/CN=Google Internet Authority G2'
'client-ssl-cert-subject' => '/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googleapis.com'
'client-ssl-cipher' => 'ECDHE-RSA-AES128-GCM-SHA256'
'client-ssl-socket-class' => 'IO::Socket::SSL'
'connection' => 'close'
'content-type' => 'application/json; charset=UTF-8'
'date' => 'Mon, 27 Oct 2014 17:48:14 GMT'
'etag' => '"etag"'
'expires' => 'Fri, 01 Jan 1990 00:00:00 GMT'
'pragma' => 'no-cache'
'server' => 'GSE'
'vary' => ARRAY(0x20311b0)
0 'Origin'
1 'Referer'
2 'X-Origin'
'x-content-type-options' => 'nosniff'
'x-frame-options' => 'SAMEORIGIN'
'x-xss-protection' => '1; mode=block'
'_msg' => 'OK'
'_protocol' => 'HTTP/1.1'
'_rc' => 200
'_request' => HTTP::Request=HASH(0x1f5dc90)
'_content' => '{"name":{"givenName":"BBB","familyName":"BBB"}}'
'_headers' => HTTP::Headers=HASH(0x224fa08)
'::std_case' => HASH(0x1f28c90)
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
'authorization' => 'Bearer mytokenhere'
'content-length' => 47
'user-agent' => 'libwww-perl/6.05'
'_method' => 'PUT'
'_uri' => URI::https=SCALAR(0x1cbc8b8)
-> 'https://www.googleapis.com/admin/directory/v1/users/user#googletestdomain'
'_uri_canonical' => URI::https=SCALAR(0x1cbc8b8)
-> REUSED_ADDRESS
Here's a sample of the code used:
#!/usr/bin/perl -w
use JSON;
use LWP::UserAgent;
my $auth_token = 'myauthtoken';
my $changes = {
'name' => {
'givenName' => 'BBB',
},
};
my $json = new JSON;
my $ur = $json->encode($changes,{utf8 => 1});
my $url = 'https://www.googleapis.com/admin/directory/v1/users/user#googletestdomain';
my $ua = LWP::UserAgent->new(timeout => 30);
my $res = $ua->put($url,
'Authorization' => 'Bearer '.$auth_token,
'Content' => $ur,
);
Setting the Content-Type header in the PUT request to application/json fixed the problem.

LWP::UserAgent and proxy error

I have a proxy error when using LWP::UserAgent
this is the code:
my $ua = LWP::UserAgent->new();
$ua->proxy( http => $ENV{HTTP_PROXY});
print Dumper($ua);
my $request = new HTTP::Request('GET', $link);
print Dumper( $request );
and this is the dumper for UserAgent
$VAR1 = bless( {
'max_redirect' => 7,
'protocols_forbidden' => undef,
'show_progress' => undef,
'handlers' => {
'response_header' => bless( [
{
'owner' => 'LWP::UserAgent::parse_head',
'callback' => sub { "DUMMY" },
'm_media_type' => 'html',
'line' => 'C:/Perl/lib/LWP/UserAgent.pm:612'
}
], 'HTTP::Config' ),
'request_preprepare' => bless( [
{
'owner' => 'LWP::UserAgent::proxy',
'callback' => sub { "DUMMY" },
'line' => 'C:/Perl/lib/LWP/UserAgent.pm:920'
}
], 'HTTP::Config' )
},
'no_proxy' => [],
'protocols_allowed' => undef,
'local_address' => undef,
'use_eval' => 1,
'requests_redirectable' => [
'GET',
'HEAD'
],
'timeout' => 90,
'def_headers' => bless( {
'user-agent' => 'libwww-perl/5.837'
}, 'HTTP::Headers' ),
'proxy' => {
'http' => 'http://igate:8080'
},
'max_size' => undef
}, 'LWP::UserAgent' );
And this is for the request:
$VAR1 = bless( {
'_content' => '',
'_uri' => bless( do{\(my $o = 'https://some_link')}, 'URI::https' ),
'_headers' => bless( {}, 'HTTP::Headers' ),
'_method' => 'GET'
}, 'HTTP::Request' );
the problem is that the response is an error:
FAIL response, 500 proxy connect failed: PROXY ERROR HEADER, could be non-SSL URL:
HTTP/1.1 503 Service Unavailable
I'm using ActiveState perl 5.10.1 on a WinXP machine
when accessing the link from browser it work
Can somebody help?
Thanks
I've always needed to set https_proxy (rather than just http_proxy) to work with SSL URIs.