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!"}
This is how that would look with HTTP::Tiny:
If you set the
contentto a hash it will default to use a multipart form encoding, and if any of the values supports aslurpmethod (in this case, the IO::Path you get by callingIOon your path) that will be used to get the value before sending.And the output so you can inspect what was sent: