in reply to Asking for help with simple api post via perl

Using my convenient curl-to-LWP service, I get the following code:

#!perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new( 'send_te' => '0' ); my $r = HTTP::Request->new( 'POST' => 'https://api.mysite.com/2022-01-08/enrich/upload', [ 'Accept' => '*/*', 'Authorization' => 'Basic YW55X3N0cmluZzp7c2VjdXJlX3Rva2VufQ= +=', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '52', 'Content-Type' => 'application/x-www-form-urlencoded' ], "data=testme\x40mysite.com&webhook=http://mywebhook.com/" ); my $res = $ua->request( $r, ); __END__ Created from curl command line curl -X POST https://api.mysite.com/2022-01-08/enrich/upload -u any_s +tring:{secure_token} -d data="testme@mysite.com" -d webhook="http://m +ywebhook.com/"

The main difference is that the content type is application/x-www-form-urlencoded instead of form-data (which I don't know). I don't see where else the generated code is substantially different from your (non-working) Perl code, but if the generated code works where yours doesn't, maybe it is the Content-Type header or that the receiving site cares about the Curl User-Agent header or something like that...

Replies are listed 'Best First'.
Re^2: Asking for help with simple api post via perl
by EPSS (Initiate) on Mar 16, 2022 at 22:41 UTC
    Thanks for the quick reply. It turns out to be how $ua->credentials($user, $pwd) as encoding the credentials. When I delete the credentials line and use $url="https://$user:$pwd\@api.mysite.com/2022-01-08/enrich/upload" instead, it works. So this is what I ended up with.
    #!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; use Data::Dumper; my $ua = LWP::UserAgent->new(); my $user = "any_string"; my $pwd = "{secure_token}"; my $url = "https://$user:$pwd\@api.mysite.com/2022-01-08/enrich/upload +"; my $data = 'testme@mysite.com'; my $webhook = "http://mywebhook.com/"; my $content_type = "form-data"; my $response = $ua->post($url, Content_Type => $content_type, Content +=> [data => $data, webhook => $webhook] ); my $content = $response->as_string(); print ($content);