Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Translate curl to LWP

by Anonymous Monk
on Nov 13, 2018 at 14:04 UTC ( #1225723=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can you help me translate this Curl command to LWP::UserAgent syntax?

curl -X POST \ -u "apikey:xxx" \ --header "Content-Type: audio/flac" \ --data-binary @{path_to_file}audio-file.flac \ " +"

The following doesn't work (obviously, as I do not know - as you can see - how to formulate a similar request with LWP

my $url=" +ecognize"; my $apikey="xxx"; open($fh, "<", "audio-file.flac") or die "Can't read file: $!"; my $audio = do { local $/; <$fh> }; close($fh); $ua = LWP::UserAgent->new(); $response = $ua->post( $url, apikey => $apikey, Content_Type => 'audio/flac', Content => $audio );

Replies are listed 'Best First'.
Re: Translate curl to LWP
by Corion (Pope) on Nov 13, 2018 at 17:52 UTC

    I have a very rudimentary module HTTP::Request::FromCurl, as inspired by Your Mother. It's not yet on CPAN but can at least somewhat rudimentary convert Curl command lines to LWP.

    The (current) output is the following prototype code, which is close to what you already have:

    my $ua = WWW::Mechanize->new(); my $r = HTTP::Request->new( 'POST' => ' +t/api/v1/recognize', [ 'Accept' => '*/*', 'Authorization' => 'Basic YXBpa2V5Onh4eA==', 'Host' => '', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '298', 'Content-Type' => 'audio/flac', ], '... that file body ...' ); my $res = $ua->request( $r, );

    To convert the above to something closer to how WWW::Mechanize would be used, just use the ->credentials method:

    my $ua = WWW::Mechanize->new(); $ua->credentials('apikey','that_api_key'); my $r = HTTP::Request->new( 'POST' => ' +t/api/v1/recognize', [ 'Accept' => '*/*', 'Host' => '', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => length $file_content, 'Content-Type' => 'audio/flac', ], $file_content ); my $res = $ua->request( $r );
Re: Translate curl to LWP
by holli (Monsignor) on Nov 13, 2018 at 16:40 UTC
    Please run your curl command with an additional "-i" switch and post the output. This will tell us about the headers and the body curl sends.


    You can lead your users to water, but alas, you cannot drown them.

      Hi Holli. I need to work on a machine without curl, this is the reason why I am trying to port this crul command.

        holli@DESKTOP-46NMP4B:~$ curl -v -X POST -i -u "apikey:xxx" --header " +Content-Type: audio/flac" --data-binary x.flac "https://stream-fra.wa" Note: Unnecessary use of -X or --request, POST is already inferred. * Trying * TCP_NODELAY set * Connected to ( port 443 + (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * successfully set certificate verify locations: * CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs * TLSv1.2 (OUT), TLS handshake, Client hello (1): * TLSv1.2 (IN), TLS handshake, Server hello (2): * TLSv1.2 (IN), TLS handshake, Certificate (11): * TLSv1.2 (IN), TLS handshake, Server key exchange (12): * TLSv1.2 (IN), TLS handshake, Server finished (14): * TLSv1.2 (OUT), TLS handshake, Client key exchange (16): * TLSv1.2 (OUT), TLS change cipher, Client hello (1): * TLSv1.2 (OUT), TLS handshake, Finished (20): * TLSv1.2 (IN), TLS handshake, Finished (20): * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384 * ALPN, server accepted to use http/1.1 * Server certificate: * subject: C=US; ST=New York; L=Armonk; O=INTERNATIONAL BUSINESS MACH +INES CORPORATION; CN=* * start date: Jan 9 00:00:00 2018 GMT * expire date: Mar 9 12:00:00 2020 GMT * subjectAltName: host "" matched cert's + "*" * issuer: C=US; O=DigiCert Inc;; CN=GeoTrust RSA +CA 2018 * SSL certificate verify ok. * Server auth using Basic with user 'apikey' > POST /speech-to-text/api/v1/recognize HTTP/1.1 > Host: > Authorization: Basic YXBpa2V5Onh4eA== > User-Agent: curl/7.58.0 > Accept: */* > Content-Type: audio/flac > Content-Length: 6 > * upload completely sent off: 6 out of 6 bytes < HTTP/1.1 401 Unauthorized HTTP/1.1 401 Unauthorized < Date: Tue, 13 Nov 2018 18:52:41 GMT Date: Tue, 13 Nov 2018 18:52:41 GMT < Content-Type: application/json Content-Type: application/json < Content-Length: 37 Content-Length: 37 < Connection: keep-alive Connection: keep-alive * Authentication problem. Ignoring this. < Www-Authenticate: Basic realm="IBM Watson Gateway(Log-in)" Www-Authenticate: Basic realm="IBM Watson Gateway(Log-in)" < * Connection #0 to host left intact {"code":401, "error": "Unauthorized"}
        Apparently the authentication with the server is not working. See here for on how to send them credentials properly.


        You can lead your users to water, but alas, you cannot drown them.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1225723]
Approved by Eily
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-12-12 18:23 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (60 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!