Unable to post messages using Mailbox API and Mojo::UserAgent

189 views Asked by At

According to the API docs (https://documentation.mailgun.com/api-sending.html) all the relevant parameters are supplied, but it gives me

400 response: BAD REQUEST

Here's my piece of code:

#!/usr/bin/perl

use Mojo::UserAgent;
use MIME::Base64;
use JSON qw(to_json);

use strict;
use warnings;
use v5.10;

my $ua = Mojo::UserAgent->new;
my $endpoint = 'https://api.mailgun.net/v3/sandbox2ad5b70fd744416ea7ff3d5422YYYYYY.mailgun.org/messages';

my $key = 'key-d3d8d350d4ef9c92349df62208XXXXXX';
my $headers = { 'Authorization' => 'Basic ' . encode_base64('api:' . $key)  };
my $params = {
    'to' => '[email protected]',
    'subject' => 'testing',
    'text' => 'some text',
    'from' => '[email protected]'
};

my $tx = $ua->post($endpoint, $headers, json => $params);
my $res = $tx->success;
if ($res) {
    say $res->body;
} else {
    my $err = $tx->error;
    die "$err->{code} response: $err->{message}" if $err->{code};
    die "Connection error: $err->{message}";
}

I have Mojo version as follows:

CORE

Perl (v5.22.1, linux)

Mojolicious (7.26, Doughnut)

OPTIONAL

EV 4.0+ (4.22)

IO::Socket::Socks 0.64+ (0.67)

IO::Socket::SSL 1.94+ (2.024)

Net::DNS::Native 0.15+ (n/a)

I wrote another version of this script using LWP::UserAgent and it works fine.

Are there some Mojo::UserAgent experts who might have an idea of what is wrong with the script?

UPDATED

Here's my LWP::UserAgent version which works without problems:

my ($key, $domain, $from, $from_name, $to, $subject, $comments) = @_;

my $url = 'https://api.mailgun.net/v3';
$url = $url . '/' . $domain . '/messages';  

my $ua = LWP::UserAgent->new;
$ua->default_header('Authorization' => 'Basic ' . encode_base64('api:' . $key));

my $data = {
      to => $to,
      subject => $subject,
      text => $comments,
      from =>  $from_name . '<' . $from . '>'        
};

my $r = $ua->post($url, Content => $data);
my $rc = $r->code; 
if ($rc == 200) {
    my $hash = from_json($r->decoded_content);
    say $hash->{id};
    say $hash->{message};       
} else {
    return { error => $rc };
}

UPDATED ON 25.02.2017

I used fake requests to my localhost:9000. Here's what I've traced using nc -l 9000:

POST / HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Authorization: Basic YXBpOmtleS1kM2Q4ZDM1MGQ0ZWY5YzkyMzQ5ZGY2MjIwOGRXXXXXX== 
Host: localhost:9000
User-Agent: libwww-perl/6.15
Content-Length: 144
Content-Type: application/x-www-form-urlencoded

text=%3Chtml%3E%3Cbody%3E%3Cp%3Etest%3C%2Fp%3E%3C%2Fbody%3E%3C%2Fhtml%3E&from=John%3Clala%40ya.ru%3E&to=zozoba29a%40yandex.ru&subject=My+Subject

And:

POST / HTTP/1.1
Host: localhost:9000
Accept-Encoding: gzip
Content-Type: application/x-www-form-urlencoded
Authorization: Basic YXBpOmtleS1kM2Q4ZDM1MGQ0ZWY5YzkyMzQ5ZGY2MjIwOGRjXXXXXX==

Content-Length: 144
User-Agent: Mojolicious (Perl)

from=John%3Clala%40ya.ru%3E&subject=My+Subject&text=%3Chtml%3E%3Cbody%3E%3Cp%3Etest%3C%2Fp%3E%3C%2Fbody%3E%3C%2Fhtml%3E&to=zozoba29a%40yandex.ru
0

There are 0 answers