Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C代码中使用curl库连接到wit api url时出错_C_Http_Post_Curl_Libcurl - Fatal编程技术网

在C代码中使用curl库连接到wit api url时出错

在C代码中使用curl库连接到wit api url时出错,c,http,post,curl,libcurl,C,Http,Post,Curl,Libcurl,目的:使用C语言使用curl库获取Wit API的响应 源代码: #include <stdio.h> #include <string.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; struct curl_slist *list = NULL; static const char *postthis="q=hello"; curl = cur

目的:使用C语言使用curl库获取Wit API的响应

源代码:

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;
  struct curl_slist *list = NULL;

  static const char *postthis="q=hello";

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://api.wit.ai/message);

    list = curl_slist_append(list, "Authorization: Bearer $TOKEN");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);

/* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
   itself */ 
   curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));

/* Perform the request, res will get the return code */ 
res = curl_easy_perform(curl);
/* Check for errors */ 
  if(res != CURLE_OK)
   fprintf(stderr, "curl_easy_perform() failed: %s\n",
          curl_easy_strerror(res));

/* always cleanup */ 
   curl_slist_free_all(list);
   curl_easy_cleanup(curl);
  }
return 0;
}
#包括
#包括
#包括
内部主(空)
{
卷曲*卷曲;
卷曲编码;
struct curl\u slist*list=NULL;
静态常量char*posthis=“q=hello”;
curl=curl_easy_init();
if(curl){
curl\u easy\u setopt(curl,CURLOPT\u URL,“https://api.wit.ai/message);
列表=curl\u slist\u append(列表,“授权:持票人$TOKEN”);
curl\u easy\u setopt(curl,CURLOPT\u HTTPHEADER,list);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,postthis);
/*如果我们不提供POSTFIELDSIZE,libcurl将通过
本身*/
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,(long)strlen(posthis));
/*执行请求时,res将获得返回代码*/
res=旋度(curl)\u容易执行(curl);
/*检查错误*/
如果(res!=卷曲(OK)
fprintf(stderr,“curl\u easy\u perform()失败:%s\n”,
卷曲(容易的);
/*始终清理*/
卷曲列表(列表);
旋度\轻松\清洁(旋度);
}
返回0;
}
$TOKEN是Wit Api的身份验证令牌


错误:错误身份验证,请检查令牌/params

$token
应该是一个shell环境变量,还是仅仅是您显示的代码中的占位符?另外,在发布代码时,如果不是关于生成错误,请确保它编译,否则其他错误可能会分散您的注意力(这是“可验证”的一部分)“在中的意思)。你好,Jachem,谢谢你的回复,$TOKEN是一个数字而不是shell env变量,我从我的Wit API传递这个数字,我没有得到编译错误,而是一个运行时错误。不清楚,
$TOKEN
从何而来,它是逐字传递到http头的吗,也就是说,作为
$TOKEN
?wit api生成了TOKEN no,我将其粘贴到代码中$TOKEN的位置。我也收到了这个错误,但是随机的。我已经做了两次完全相同的卷曲要求;一旦我得到这个错误,一旦它起作用。我怀疑超时问题可能是错误的错误消息。
$TOKEN
应该是一个shell环境变量,还是只是显示的代码中的占位符?此外,在发布代码时,请确保它是编译的,否则其他错误可能会分散您的注意力(这是中“可验证”的部分含义)。你好,Jachem,感谢您的回复,$TOKEN是一个数字,而不是shell env变量,我从我的Wit API传递这个数字,我没有得到编译错误,而是一个运行时错误。不清楚
$TOKEN
从何而来,它是逐字传递到http头的吗,即作为
$TOKEN
?Wit API生成令牌否我将它粘贴到代码中$TOKEN的位置。我也得到了这个错误,但是随机的。我已经做了两次完全相同的卷曲要求;一旦我得到这个错误,一旦它起作用。我怀疑这可能是超时问题的错误消息。