Following Perl code generates an error printed below:
use strict;
use Data::Dumper;
use LWP::UserAgent;
use JSON;
my $token = 'my token';
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(PUT => "endpoint");
$req->header( 'Authorization' => "Bearer $token" );
$req->content_type('application/json');
$req->content('{"text":"whiteboard"}');
my $res = $ua->request($req);
if ($res->is_success) {
my $content = $res->decoded_content;
my $fromjson = from_json($content);
print Dumper $fromjson->{'results'};
}
else {
print $res->status_line, "\n";
print $res->content, "\n";
}
Error:
{"detail":[{"loc":["body"],"msg":"str type expected","type":"type_error.str"}]}
However if I write the same code in Python, it works:
import requests
import os
import json
url = 'endpoint'
token='my token'
headers = {
"Authorization": "Bearer "+token[:-1],
"Content-type" : "application/json"
}
res=requests.put(url, json='{"text":"whiteboard"}', headers=headers)
#res=requests.put(url, json='test string', headers=headers) # this also works
print('Response Content:\n',res)
What am I missing in the Perl code?
"my toke". In the perl version it's"my token". I don't think this is the root cause, but it is a difference.$req->as_stringto see what's actually being sent.