2

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' => 'abc@domain.ru',
    'subject' => 'testing',
    'text' => 'some text',
    'from' => 'postmaster@sandbox2ad5b70fd744416ea7ff3d5422YYYYYY.mailgun.org'
};

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
Community
  • 1
  • 1
varnie
  • 2,523
  • 3
  • 35
  • 42
  • Can you show your LWP::UserAgent script as well please? – simbabque Feb 24 '17 at 15:35
  • @simbabque I updated my question. – varnie Feb 24 '17 at 15:37
  • The `from` field has different content. – simbabque Feb 24 '17 at 15:39
  • yes, I tried that "format", it had no impact. – varnie Feb 24 '17 at 15:39
  • Please make sure you have the exact same thing in both versions so we don't waste time trying things you already tried. – simbabque Feb 24 '17 at 15:40
  • 3
    Apart from different `from` field the Mojo version uses a JSON body while the LWP version behaves like a form submission, i.e. `application/x-www-form-urlencoded`. I cannot see from the documentation that a JSON submission is accepted at all. – Steffen Ullrich Feb 24 '17 at 15:46
  • @SteffenUllrich I already tried `my $tx = $ua->post($url, $headers, form => $params);` instead, it didn't change anything at all. Please correct me if I am submitting a form improperly. – varnie Feb 24 '17 at 15:57
  • @varnie: I just pointed out that your non-working Mojo version does obviously different things than the working LWP version. I recommend that you let it do exactly the same things (i.e. same from etc) first so that you can better find out what difference causes the problems. Finding the cause of a problem when changing lots of things at the same time is much harder than finding it when only changing few things. – Steffen Ullrich Feb 24 '17 at 16:11
  • 1
    It might also be helpful to dump out the full request with headers and body for both of the programs and compare. – simbabque Feb 24 '17 at 16:25
  • @simbabque I provided the dumped data. – varnie Feb 25 '17 at 13:17
  • 1
    The mojo request doesn't have a body, but it has a content length header. Did you forget to copy that or is it missing? – simbabque Feb 25 '17 at 13:33
  • Ouch, my bad. I forgot to paste it. Updated again. – varnie Feb 25 '17 at 13:36

0 Answers0