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

Hello

I am struggling converting a known working curl command (below) to a perl script. The curl command is first with my attempt following but I keep getting a 403 permission denied, even though curl works

curl -H "Authorization: PASSWORD" -H "Content-Type: application/json" + -d '{"message":"Test message"}' https://myurl.url.url/api/v1/action +s/message/

perl attempt

use LWP::UserAgent; use strict; my $url=" https://myurl.url.url/api/v1/actions/message"; my $ua = LWP::UserAgent->new; my $response = $ua->post($url, 'Content-type' => 'application/json', 'Authorization' => 'PASSWORD', 'data' => {"message":"Test message"} ); print $response->as_string;

Pointers welcome!

Replies are listed 'Best First'.
Re: curl to perl POST
by choroba (Cardinal) on Jan 25, 2019 at 14:18 UTC
    Using Corion's HTTP::Request::FromCurl:
    curl2lwp.pl -H "Authorization: PASSWORD" -H "Content-Type: applicatio +n/json" -d '{"message":"Test message"}' https://myurl.url.url/api/v1 +/actions/message/ my $ua = WWW::Mechanize->new(); my $r = HTTP::Request->new( 'POST' => 'https://myurl.url.url/api/v1/actions/message/', [ 'Accept' => '*/*', 'Authorization' => 'PASSWORD', 'Host' => 'myurl.url.url:443', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '26', 'Content-Type' => 'application/json', ], '{"message":"Test message"}' ); my $res = $ua->request( $r, );

    Update: See https://corion.net/curl2lwp.psgi for an online version (Thanks Discipulus).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: curl to perl POST
by hippo (Archbishop) on Jan 25, 2019 at 14:24 UTC
    'data' => {"message":"Test message"}

    Use the 'Content' key as specified in the documentation for post if you want to supply the content in this way. 'data' is not a special key. Also, don't supply the content as a hashref - use a string instead just like you did with the curl command.

Re: curl to perl POST
by karlgoethebier (Abbot) on Jan 25, 2019 at 14:20 UTC