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

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 \ "https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize +"

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="https://stream-fra.watsonplatform.net/speech-to-text/api/v1/r +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' => 'https://stream-fra.watsonplatform.net/speech-to-tex +t/api/v1/recognize', [ 'Accept' => '*/*', 'Authorization' => 'Basic YXBpa2V5Onh4eA==', 'Host' => 'stream-fra.watsonplatform.net:443', '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' => 'https://stream-fra.watsonplatform.net/speech-to-tex +t/api/v1/recognize', [ 'Accept' => '*/*', 'Host' => 'stream-fra.watsonplatform.net:443', '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.


    holli

    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 +tsonplatform.net/speech-to-text/api/v1/recognize" Note: Unnecessary use of -X or --request, POST is already inferred. * Trying 158.177.93.238... * TCP_NODELAY set * Connected to stream-fra.watsonplatform.net (158.177.93.238) 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=*.watsonplatform.net * start date: Jan 9 00:00:00 2018 GMT * expire date: Mar 9 12:00:00 2020 GMT * subjectAltName: host "stream-fra.watsonplatform.net" matched cert's + "*.watsonplatform.net" * issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; 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: stream-fra.watsonplatform.net > 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 stream-fra.watsonplatform.net 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.


        holli

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1225723]
Approved by Eily
Front-paged by haukex
help
Chatterbox?
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
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (60 votes). Check out past polls.

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