EPSS has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to perform the following curl command in perl and I can't figure out what I doing wrong. Any suggestions you have would be appreciated. Here is my non-working perl code and working php code. In both the perl and php scripts the site returns a success status but in the perl code they don't get the data and webhook.

curl -X POST https://api.mysite.com/2022-01-08/enrich/upload -u any_string:{secure_token} -d data="testme@mysite.com" -d webhook="http://mywebhook.com/"

Non-Working Perl Code

#!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; use Data::Dumper; my $ua = LWP::UserAgent->new(); my $url = "https://api.mysite.com/2022-01-08/enrich/upload"; my $data = 'testme@mysite.com'; my $webhook = "http://mywebhook.com/"; my $user = "any_string"; my $pwd = "{secure_token}"; my $content_type = "form-data"; $ua->credentials($user, $pwd); my $response = $ua->post($url, Content_Type => $content_type, Content +=> [data => $data, webhook => $webhook] ); my $content = $response->as_string(); print ($content);

Working PHP Code

<?php $url = "https://api.mysite.com/2022-01-08/enrich/upload"; $data = "testme@mysite.com"; $webhook = "http://mywebhook.com/"; $user = "any_string"; $pwd = "{secure_token}"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERPWD, "$user:$pwd"); $data = "data=$data&webhook=$webhook"; curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //for debug only! curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $resp = curl_exec($curl); curl_close($curl); var_dump($resp); ?>

Replies are listed 'Best First'.
Re: Asking for help with simple api post via perl
by Corion (Patriarch) on Mar 16, 2022 at 21:49 UTC

    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...

      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);