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

In bash I send HTTP request and get response like this:

#!/bin/bash curl -kv -X POST \ "https://my.webpage.com/my/path" \ --header 'My-Header: alfa' \ --header 'My-Header-2: beta' \ --data-raw 'here is my data'

How exactly can I do that in Perl?

#!/usr/bin/perl use Mojo::Message::Request; my $req = Mojo::Message::Request->new; $req->url->parse("https://my.webpage.com/my/path"); $req->method("POST"); $req->headers->header("My-Header", "alfa"); $req->headers->header("My-Header-2", "beta"); $req->body("here is my data"); ?? what here? ?? how to send that request to serwer my.webpage.com? ?? hot do I get response from server?

Replies are listed 'Best First'.
Re: Mojo instead of curl
by Corion (Patriarch) on Apr 14, 2023 at 11:52 UTC

    See also my converter from curl to Perl at https://corion.net/curl2lwp.psgi:

    #!perl use strict; use warnings; use HTTP::Tiny; my $ua = HTTP::Tiny->new(); my $res = $ua->request( 'POST' => 'https://my.webpage.com/my/path', { headers => { 'Content-Length' => '15', 'User-Agent' => 'curl/7.55.1', 'My-Header-2' => 'beta', 'My-Header' => 'alfa', 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*' }, content => "here is my data" }, ); __END__ Created from curl command line curl -kv -X POST "https://my.webpage.com/my/path" --header 'My-Header: alfa' --header 'My-Header-2: beta' --data-raw 'here is my data'

    But your question gives me an idea - I should also add a converter option to output Mojolicious code.

    Update: The app now also supports Mojolicious output:

    #!perl use strict; use warnings; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new( 'insecure' => '1' ); my $tx = $ua->build_tx( 'POST' => 'https://my.webpage.com/my/path', { 'Accept' => '*/*', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '15', 'Content-Type' => 'application/x-www-form-urlencoded', 'My-Header' => 'alfa', 'My-Header-2' => 'beta' }, "here is my data" ); my $res = $ua->start($tx)->result; __END__ Created from curl command line curl -kv -X POST "https://my.webpage.com/my/path" --header 'My-Header: alfa' --header 'My-Header-2: beta' --data-raw 'here is my data'

        No, the generated code is correct as is, as the OP used the -k command line option, which disables the SSL verification :)

Re: Mojo instead of curl
by Fletch (Bishop) on Apr 14, 2023 at 10:41 UTC

    You need a Mojo::UserAgent to use to retrieve things. See the USER AGENT section of the tutorial for more examples.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Mojo instead of curl
by alexander_lunev (Pilgrim) on Apr 14, 2023 at 16:06 UTC

    As already said, you need Mojo::UserAgent for such things in Perl, but you can also do it like this:

    perl -Mojo -E "say g('https://my.webpage.com/my/path' =>\ { 'My-Header' => 'alfa', 'Me-Header-2' => 'beta' } => 'Hi!')->body"
Re: Mojo instead of curl
by 1nickt (Canon) on Apr 14, 2023 at 17:07 UTC

    It all depends on the context, but Mojo::UserAgent has a lot of dependencies and takes an age to load compared to HTTP::Tiny.

    The following table shows time in seconds to execute Mojo::UserAgent->new() / HTTP::Tiny->new().

    Mojo::UserAgentHTTP::Tiny
      0.000] Mojo::UserAgent
      0.000] Mojo::Base
      0.001] utf8
      0.002] feature
      0.004] mro
      0.006] Carp
      0.007] overloading
      0.010] Scalar::Util
      0.010] List::Util
      0.012] constant
      0.013] warnings::register
      0.014] Role::Tiny
      0.018] Future::AsyncAwait
      0.019] Mojo::Util
      0.020] Data::Dumper
      0.025] bytes
      0.027] Digest::MD5
      0.028] Digest::base
      0.030] Digest::SHA
      0.031] vars
      0.032] Fcntl
      0.033] integer
      0.036] Encode
      0.038] Encode::Alias
      0.041] Encode::MIME::Name
      0.042] Storable
      0.043] Log::Agent
      0.047] parent
      0.048] Encode::Encoding
      0.049] Encode::Config
      0.050] Encode::ConfigLocal
      0.051] File::Basename
      0.053] Getopt::Long
      0.061] overload
      0.063] IO::Compress::Gzip
      0.063] IO::Compress::RawDeflate
      0.064] IO::Compress::Base
      0.065] IO::Compress::Base::Common
      0.066] File::GlobMapper
      0.066] File::Glob
      0.073] IO::File
      0.074] Symbol
      0.075] SelectSaver
      0.076] IO::Seekable
      0.076] IO::Handle
      0.077] IO
      0.083] IO::Compress::Adapter::Deflate
      0.084] Compress::Raw::Zlib
      0.090] IO::Compress::Gzip::Constants
      0.091] IO::Compress::Zlib::Extra
      0.094] IO::Poll
      0.094] IO::Uncompress::Gunzip
      0.095] IO::Uncompress::RawInflate
      0.096] IO::Uncompress::Base
      0.101] IO::Uncompress::Adapter::Inflate
      0.104] MIME::Base64
      0.106] Pod::Usage
      0.107] Config
      0.108] File::Spec
      0.108] File::Spec::Unix
      0.109] Cwd
      0.115] Pod::Text
      0.116] Pod::Simple
      0.117] Pod::Escapes
      0.118] Pod::Simple::LinkSection
      0.119] Pod::Simple::BlackBox
      0.121] if
      0.140] Socket
      0.143] Sub::Util
      0.143] Unicode::Normalize
      0.154] Mojo::EventEmitter
      0.155] Mojo::IOLoop
      0.155] Mojo::IOLoop::Client
      0.156] Errno
      0.157] IO::Socket::IP
      0.157] base
      0.158] IO::Socket
      0.160] IO::Socket::INET
      0.162] IO::Socket::UNIX
      0.164] POSIX
      0.168] Tie::Hash
      0.175] Mojo::IOLoop::TLS
      0.176] Mojo::File
      0.177] File::Copy
      0.179] File::Find
      0.183] File::Path
      0.186] File::Spec::Functions
      0.187] File::stat
      0.188] Class::Struct
      0.191] File::Temp
      0.195] Carp::Heavy
      0.196] Mojo::Collection
      0.197] re
      0.201] Mojo::ByteStream
      0.205] IO::Socket::SSL
      0.206] Net::SSLeay
      0.207] AutoLoader
      0.216] IO::Socket::SSL::PublicSuffix
      0.217] URI::_idna
      0.217] URI::_punycode
      0.223] Config_heavy.pl
      0.225] Config_git.pl
      0.248] Net::DNS::Native
      0.248] IO::Socket::Socks
      0.250] Mojo::IOLoop::Server
      0.253] Mojo::IOLoop::Stream
      0.254] Mojo::IOLoop::Subprocess
      0.255] Mojo::JSON
      0.256] JSON::PP
      0.256] JSON::PP::Boolean
      0.265] Cpanel::JSON::XS
      0.269] attributes
      0.274] Mojo::Promise
      0.274] Mojo::Exception
      0.279] Mojo::Reactor::Poll
      0.280] Mojo::Reactor
      0.281] Mojo::Loader
      0.286] Mojo::Reactor::EV
      0.286] EV
      0.287] Mojo::UserAgent::CookieJar
      0.288] Mojo::Cookie::Request
      0.289] Mojo::Cookie
      0.290] Mojo::Path
      0.292] Mojo::UserAgent::Proxy
      0.293] Mojo::URL
      0.293] Mojo::Parameters
      0.297] Mojo::UserAgent::Server
      0.298] Mojo::Server::Daemon
      0.298] Mojo::Server
      0.300] Mojo::Transaction::WebSocket
      0.300] Mojo::Transaction
      0.301] Mojo::Message::Request
      0.301] Mojo::Message
      0.302] Mojo::Asset::Memory
      0.302] Mojo::Asset
      0.303] Mojo::Asset::File
      0.304] Mojo::Content::Single
      0.305] Mojo::Content
      0.306] Mojo::Headers
      0.310] Mojo::Content::MultiPart
      0.312] Mojo::DOM
      0.312] Mojo::DOM::CSS
      0.315] Mojo::DOM::HTML
      0.321] Mojo::JSON::Pointer
      0.322] Mojo::Upload
      0.326] Mojo::Message::Response
      0.327] Mojo::Cookie::Response
      0.327] Mojo::Date
      0.328] Time::Local
      0.332] Mojo::WebSocket
      0.337] Mojo::UserAgent::Transactor
      0.338] Mojo::Transaction::HTTP
    
      0.000] HTTP::Tiny
      0.004] bytes
      0.005] Errno
      0.006] Config
      0.007] IO::Socket
      0.008] IO::Handle
      0.009] Carp
      0.011] overloading
      0.014] Symbol
      0.015] SelectSaver
      0.016] IO
      0.019] Socket
      0.020] warnings::register
      0.026] IO::Socket::INET
      0.028] IO::Socket::UNIX
      0.033] IO::Socket::IP
      0.034] base
      0.036] POSIX
      0.037] Fcntl
      0.043] Tie::Hash
      0.045] constant
    

    The way forward always starts with a minimal test.
      To be fair, HTTP::Tiny will need to load more modules if you give it a https: URL. And don't forget verify_SSL => 1 or HTTP::Tiny doesn't actually give you security.