卷曲api解析

Curl api parsing

正在尝试解析 curl api

但收到此错误 401 Unauthorized

不知道问题出在哪里

因为身份验证细节没问题

这是卷曲

curl 'https://api.twilio.com/2010-04-01/Accounts/SID/Messages.json' \
   -X POST \
   --data-urlencode 'To=whatsapp:+123456789' \
   --data-urlencode 'From=whatsapp:+1234567890' \
   --data-urlencode 'Body=Your Yummy Cupcakes Company order of 1 dozen frosted cupcakes has shipped and should be delivered on July 10, 2019. Details: http://www.yummycupcakes.com/' \
   -u SID:[AuthToken]
#!/usr/bin/perl

use strict;
use warnings;
use HTTP::Tiny;
use JSON;
use Data::Dumper;

my $url = "https://api.twilio.com/2010-04-01/Accounts/SID/Messages.json";
my $json = encode_json {
To    => 'whatsapp:+256775562361',
From => 'whatsapp:+14155238886',
Body   => 'new test msg',
};

my $http = HTTP::Tiny->new(
default_headers => {
Authorization => 'SID:TOKEN',
}
);

my $response = $http->post( $url => {
content => $json,
headers => { 'Content-Type' => 'application/json' },
});

if ( $response->{'is_success'} ) {
print Dumper( decode_json $response->{'content'} );
} else {
print "$response->{'status'} $response->{'reason'}\n";
}

Authorization”header 的格式不是 <user>:<pass> -- 它是 <auth-scheme> <authorization-parameters>curl 正在为您做这件事,但它基本上是:

Authorization: Basic <mime-encoded "<user>:<pass>">

我为此使用 URI 模块:

my $uri = URI->new("https://api.twilio.com/...");
$uri->userinfo("$sid:$auth");

$http->post( $uri => ... );