TIL: File Uploads Using the Req Elixir Library

• 1 min read

The Req Elixir library doesn’t support file uploads(as of version 0.4.5). Instead, you need to use multipart to construct the HTTP request before you send it.

Here is an example sending an mp3 file to the OpenAI audio transcription API(aka Whisper):

def transcribe_audio(file_path, token) do
    model = "whisper-1"
    filename = Path.basename(file_path)
    {:ok, file_contents} = File.read(file_path)

    multipart =
      |> Multipart.add_part(Multipart.Part.text_field(model, "model"))
      |> Multipart.add_part(
        Multipart.Part.file_content_field(filename, file_contents, :file, filename: filename)

    content_length = Multipart.content_length(multipart)
    content_type = Multipart.content_type(multipart, "multipart/form-data")

    headers = [
      {"authorization", "Bearer #{token}"},
      {"Content-Type", content_type},
      {"Content-Length", to_string(content_length)}

      headers: headers,
      body: Multipart.body_stream(multipart)
Now look what you've done 🌋
Stop clicking and run for your life! 😱
Uh oh, I don't think the system can't handle it! 🔥
Stop it, you're too kind 😄
Thanks for the love! ❤️
Thanks, glad you enjoyed it! Care to share?
Hacker News Reddit


Recommended Posts ✍🏻

See All »
• 6 min read
TIL: Sum Types With `instructor_ex`
Read Post »
• 1 min read
TIL: Creating `sentence-transformers` Embedding...
Read Post »
• 2 min read
Biology:Chemistry :: Chemistry:Physics :: Physi...
Read Post »