Processing audio from mobile app form data in AWS Lambda with Go

I have a mobile app built with React Native that sends recorded audio files to my AWS Lambda backend function (written in Go). The audio gets sent as multipart form data but I’m having trouble reading it properly on the server side.

For recording, I use react-native-audio-recorder-player with these settings:

const recordingConfig: RecordingConfig = {
  AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
  AudioSourceAndroid: AudioSourceAndroidType.MIC,
  AVModeIOS: AVModeIOSOption.measurement,
  AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
  AVNumberOfChannelsKeyIOS: 2,
  AVFormatIDKeyIOS: AVEncodingOption.wav,
};

On the client side, I create the form data like this:

const uploadData = new FormData();
uploadData.append('audioFile', {
  uri: recordingPath,
  type: 'audio/wav',
  name: 'recording.wav',
});

My Lambda function tries to process the audio data (I have BinaryMediaTypes configured in my SAM template):

audioReader := strings.NewReader(event.Body)

openaiClient := openai.NewClient(
    option.WithAPIKey(API_KEY),
)

transcription, err := openaiClient.Audio.Transcriptions.New(context.Background(), openai.AudioTranscriptionNewParams{
    Model: openai.F(openai.AudioModelWhisper1),
    File:  openai.FileParam(audioReader, "recording.wav", "audio/wav"),
})

if err != nil {
    log.Error("Transcription failed", zap.Error(err))
    return "", err
}

The issue is that my audioReader appears empty when I log it. The OpenAI API responds with an invalid file format error even though WAV should be supported. How can I properly extract the audio file from the multipart form data in my Go Lambda function?

hmm interesting problem! are you checking if the lambda is actualy receiving the file data correctly? maybe try logging event.IsBase64Encoded and the length of event.Body first to see whats coming through? also curious - have you tried using a different audio format like mp3 to rule out wav encoding issues from react native?

The primary issue stems from attempting to parse multipart form data as plain text. When Lambda receives multipart form data, the body contains boundary markers and headers that need proper parsing before extracting the actual file content. You need to use Go’s multipart package to handle this correctly. Parse the request body with multipart.NewReader() and iterate through the parts to find your audio file. The key is decoding the base64 content if event.IsBase64Encoded is true, then using multipart.Reader.NextPart() to access individual form fields. Once you locate the audio file part, read its content into a buffer and pass that to the OpenAI client instead of the raw event body. This approach properly extracts the binary audio data from the multipart wrapper, resolving both the empty reader and format validation issues you’re experiencing.

quick thought - are you handling the content-type header properly? multipart data needs Content-Type: multipart/form-data but lambda might be expecting application/json. also worth checking if your sam template has the right binary media types configured like multipart/form-data not just generic types. sometimes the issue isnt parsing but lambda gateway transformation