Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing POST request of an audio file

Tags:

raku

cro

What is the equivalent POST request using:

  • Cro
  • "HTTP::Tiny"
  • Other libraries

for this curl shell command:

curl --request POST \
  --url https://api.someservice.com/v1/ \
  --header 'Authorization: Bearer TOKEN' \
  --header 'Content-Type: multipart/form-data' \
  --form file=@/path/to/file/audio.mp3 \
  --form transformer=trans-2 \
  --form format=text

I tried following the examples in "Cro::HTTP::Client" without success...

(See the example starting with my $resp = await Cro::HTTP::Client.post: 'we.love.pand.as/pandas',)


Update

Here is a working example using the answer of @jja that requires to:

  • Have an OpenAI (account and) authorization key

  • Download one of the MP3 files from here

    • Or use another MP3 file with speech recording.
use HTTP::Tiny;


my $fileName = $*HOME ~ '/Downloads/HelloRaccoonsEN.mp3';


say .<content>.decode given HTTP::Tiny.post: 'https://api.openai.com/v1/audio/transcriptions',
        headers => { authorization => "Bearer {%*ENV<OPENAI_API_KEY>}" },
        content => {
            file        => $fileName.IO,
            model       => 'whisper-1',
            format      => 'text'
        };

Here is the expected result:

# {"text":"Raku practitioners around the world, eat more onions!"}
like image 373
Anton Antonov Avatar asked Oct 29 '25 02:10

Anton Antonov


1 Answers

It would have been helpful to know what you attempted in order to work out how to correct it. Anyway, I'd expect the example request to look something like this in Cro:

my $resp = await Cro::HTTP::Client.post: 'https://api.someservice.com/v1/',
    headers => [ Authorization => 'Bearer TOKEN' ],
    content-type => 'multipart/form-data',
    body => [
        transformer => 'trans-2',
        format => 'text',
        Cro::HTTP::Body::MultiPartFormData::Part.new(
            headers => [Cro::HTTP::Header.new(
                name => 'Content-type',
                value => 'audio/mpeg'
            )],
            name => 'file',
            filename => 'audio.mp3',
            body-blob => slurp('/path/to/file/audio.mp3', :bin)
        )
    ];

I'm not sure exactly what mime type curl goes with, so it may slightly differ from what curl does in that regard.

like image 191
Jonathan Worthington Avatar answered Nov 01 '25 01:11

Jonathan Worthington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!