2

I'm trying to use Mojo::UserAgent to access the eBay API.

One of the options is to use API requests with an XML payload, but I have had no success doing it with Mojo::UserAgent. I didn't find any options for the $ua->post method.

I also tried

my $tx = $ua->build_tx(POST => $ebay_api_url => $headers);
$tx->req->body($xml_body);
my $res = $ua->start($tx)->res->json;

with no success. The XML body is not set for the request.

What do I need to do to achieve the desired result?

I know about the possibility of using JSON requests, but that is a reserve plan.

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • did you finally find the solution? Same problem here, except in this case I already have created the JSON – jjmerelo Jan 15 '21 at 17:47

3 Answers3

1

Try to post your $xml_body like so:

my $tx = $ua->post($ebay_api_url => form => $xml_body);
ashraf
  • 537
  • 7
  • 16
  • I tried, no results, there must be a HASH after 'form =>', but I have a string with XML – Nikita Tropin Nov 24 '15 at 09:29
  • @NikitaTropin so `$ua->post($ebay_api_url => $xml_body);` wouldn't work either? – ashraf Nov 24 '15 at 18:48
  • No, it's not. `Malformed JSON: Expected string, array, object, number, boolean or null at line 0, offset 0 at /home/nikita/perl5/perlbrew/perls/perl-5.20.3/lib/site_perl/5.20.3/Mojo/Message.pm line 140`. I'm passing string in $xml_body btw. – Nikita Tropin Nov 24 '15 at 19:06
0

You likely want (2nd example in post doc):

my $tx = $ua->post($ebay_api_url => {Accept => '*/*'} => $xml_body);
codebard
  • 146
  • 1
  • 5
0

I faced the similar issue like yours but later I realized that the problem was with the xml data. Please ensure that you do not have any trailing or leading white spaces in your $xml_body. This works:

   my $tx = $ua->post($ebay_api_url => $headers => $xml_body);

Mojolicious and Mojo::UserAgent is awesome and light.

Sachin Dangol
  • 504
  • 5
  • 13