dify icon indicating copy to clipboard operation
dify copied to clipboard

/audio-to-text api not work with axios nodejs

Open vkiller opened this issue 2 years ago • 2 comments

Self Checks

  • [X] This is only for bug report, if you would like to ask a quesion, please head to Discussions.
  • [X] I have searched for existing issues search for existing issues, including closed ones.
  • [X] I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [X] Pleas do not modify this template :) and fill in all the required fields.

Dify version

0.6.2

Cloud or Self Hosted

Cloud

Steps to reproduce

Acording audio-to-text api,just need fileand userparameters,if use curl

curl -X POST 'https://api.dify.ai/v1/audio-to-text' \
--header 'Authorization: Bearer app-xxxxx' \
--form 'file=@"./33569d89d8aa7451fd1eac201.mp3";type=audio/mp3'

that's work,but when use nodejs api with axios

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs')

const form = new FormData();
form.append('file', fs.readFileSync('./33569d89d8aa7451fd1eac201.mp3'));

const main = async function() {
  const response = await axios({
    method: "post",
    url: "https://api.dify.ai/v1/audio-to-text",
    data: form,
    headers: { 
      'Authorization': 'Bearer app-xxxxxxxxxxxx',
      "Content-Type": "audio/mp3"
    },
  })
}

main()

then got

      code: 'bad_request_key_error',
      message: 'The browser (or proxy) sent a request that this server could not understand.',
      status: 400

✔️ Expected Behavior

expect the same return as curl

❌ Actual Behavior

Actual

      code: 'bad_request_key_error',
      message: 'The browser (or proxy) sent a request that this server could not understand.',
      status: 400

vkiller avatar Apr 14 '24 00:04 vkiller

The issue you're encountering with the /audio-to-text API when using Axios in Node.js is likely due to how the file is being sent. Instead of reading the file into memory and appending it to the form data, you should append the file stream directly. This is because the API expects a file object, not a buffer. Here's the corrected way to append the file:

const form = new FormData();
form.append('file', fs.createReadStream('./33569d89d8aa7451fd1eac201.mp3'), {
  filename: '33569d89d8aa7451fd1eac201.mp3',
  contentType: 'audio/mp3',
});

Additionally, you should not manually set the "Content-Type" header to "audio/mp3" when using form-data with Axios. Instead, allow form-data to automatically set the Content-Type header by including the headers from form.getHeaders() in your Axios request. This ensures the correct multipart/form-data content type and boundary are used:

const response = await axios({
  method: "post",
  url: "https://api.dify.ai/v1/audio-to-text",
  data: form,
  headers: { 
    'Authorization': 'Bearer app-xxxxxxxxxxxx',
    ...form.getHeaders()
  },
})

This adjustment should align your Axios request more closely with how the curl command works and resolve the bad_request_key_error you're seeing.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] avatar Apr 14 '24 01:04 dosubot[bot]

yes,that works,should append the file stream.thanks

vkiller avatar Apr 14 '24 01:04 vkiller