CURL 调用导致来自 ASR Web 服务的意外响应

标签 c curl libcurl

以下错误是我尝试使用 CURL 将音频样本(从内存)上传到 Nuance 的 ASR 网络服务时遇到的错误。我不确定我的代码哪里出错了,所以我希望有人能帮我找出错误所在。根据响应,错误是无效的应用程序 ID 或应用程序 key ,但我已直接从我的 Nuance 帐户粘贴该应用程序 ID 和 key 并检查了几次。

代码应该执行以下操作:

  1. 将音频数据发布到 Nuance 的 ASR 网络服务
  2. 从ASR服务获取识别结果(文本字符串)

我的源代码是这样的:

#include "curl.h"


typedef struct {
    unsigned char* buffer;       
    unsigned int   size;         
    unsigned int   index;
} transfer_state;

struct curl_slist *slist = NULL;
CURL* curl;
int status = 0;
char* audioDataPtr = NULL;
transfer_state read_state, write_state;
unsigned long sizeOfAudioInBytes;
char recognitionResult[4096] = {0};



static size_t read_data(char *bufptr, size_t size, size_t nitems, transfer_state *read_state) {
    size_t read;

    read = min(size * nitems, read_state->size - read_state->index);
    memcpy(bufptr, &(read_state->buffer[read_state->index]), read);
    read_state->index += read;

    return read / size;
}

static size_t write_data (void *ptr, size_t size, size_t nmemb, transfer_state *write_state) {

    memcpy(&(write_state->buffer[write_state->index]), ptr, size * nmemb);
    write_state->index += size * nmemb;
    //assert(write_state->index < write_state->size);

    return nmemb;
}

/* Function for loading audio */
unsigned long loadAudioFile(char* audioFileName, char** audioData)
{
   unsigned long sz = 0;

   FILE* fid = fopen(audioFileName, "r");
   fseek(fid, 0L, SEEK_END);
   sz = ftell(fid);
   fseek(fid, 0L, SEEK_SET);
   *audioData = (char*) malloc(sizeof(char) * sz);
   fread((void*) *audioData, 1, sz, fid);
   fclose(fid);

   return sz;
}

void main(void)
{
   int n = 0;
   char* char_ptr = NULL;
   char contentLength[128] = {0};

   sizeOfAudioInBytes = loadAudioFile("audio_16k16bit.pcm", &audioDataPtr);

   read_state.buffer  = (unsigned char*) audioDataPtr;
    read_state.index   = 0;
    read_state.size    = sizeOfAudioInBytes;

    write_state.buffer = (unsigned char*) recognitionResult;
    write_state.index  = 0;
    write_state.size   = 4096;

   curl = curl_easy_init();
    if (curl == NULL)
   {
      printf("[%s:%d] ERROR. \n", __FUNCTION__, __LINE__);
   }

   status = curl_easy_setopt(curl, CURLOPT_URL,  "https://dictation.nuancemobility.net/NMDPAsrCmdServlet/dictation");
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   status = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "appId=MYAPPID&appKey=MY128CHARAPPKEY"); 
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   slist = curl_slist_append(NULL, "Content-Type:audio/x-wav;codec=pcm;bit=16;rate=16000");
   curl_slist_append(slist, "Content-Language:ENUS");
   curl_slist_append(slist, "Accept-Language:ENUS");
   curl_slist_append(slist, "Accept:text/plain");
   curl_slist_append(slist, "Accept-Topic:Dictation");

   memset(contentLength,0,128);
   sprintf(contentLength, "Content-Length:%d", sizeOfAudioInBytes);
   curl_slist_append(slist, contentLength);


   status = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   status = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (char*) audioDataPtr);
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   status = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) (sizeOfAudioInBytes) );
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   status = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   status = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&write_state);
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   status = curl_easy_setopt(curl, CURLOPT_READDATA, (void*)&read_state);
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   status = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_data);
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

   status = curl_easy_perform(curl);
   if (status != 0)
   {
      printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
   }

    n = write_state.index;
    recognitionResult[n] = 0; // possible problem if n == size!

   /* Locate first occurence of \n */
    char_ptr = strchr(recognitionResult, '\n');
    if (char_ptr != 0) 
   {
      /* Zero terminate string */
        *char_ptr = 0;
    }

   printf("%s\n", recognitionResult);

   free(audioDataPtr);

    curl_slist_free_all(slist);

   curl_easy_cleanup(curl);

}

这是我执行上面的 C 代码时从服务器返回的内容:

Error 401 Unauthorized, invalid application id or key.

HTTP ERROR 401

Problem accessing /NMDPAsrCmdServlet/dictation. Reason:.

    Unauthorized,
invalid application id or key.

..

为了验证我的 ASR 帐户是否有效,我下载了一个 curl 可执行文件并执行了以下命令:

curl "https://dictation.nuancemobility.net:443/NMDPAsrCmdServlet/dictation?appId=MYAPPID&appKey=MY128BYTEAPPKEY "-H "Content-Type: audio/x-wav;codec=pcm;bit=16;rate=16000"-H "Accept-Language: ENUS"-H "Content-Length: 264522"-H "Accept: application/xml"-H "Accept-Topic: Dictation"-k --data-binary @audio_16k16bit.pcm -v

这很好用:

CURL response

最佳答案

我正在做一些非常相似的事情,我通过使用通过邮件而不是从 http://dragonmobile.nuancemobiledeveloper.com 收到的 api key 和 ID 解决了您面临的问题。

如果您能帮助解决以下错误 500:

Problem accessing /NMDPAsrCmdServlet/dictation. Reason:

x-nuance-sessionid*********************************

Received QueryRetry: 6 AUDIO_INFO

解决此问题后,当我使用 codex 发布音频文件时,我收到另一个错误 500。不知道如何解决它。

谢谢。

关于CURL 调用导致来自 ASR Web 服务的意外响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29064900/

相关文章:

curl - jira curl 解决问题

c++ - 使用 libcurl 发布数据

c++ - 如何在ubuntu上编译curlpp?

c - 如何在 C 中同时使用 GTK 和 libcurl?

python - PyCurl 替代品,libcurl 的 pythonic 包装器?

c - 如何将基于 C 的语言添加到 GCC

c - c编译器如何为结构分配内存?

c - OpenBSD 6.1 termio.h : No such file or directory

c - 获取日期并将其保存在结构体 C 中

php - PHP,线程和 curl