使用 libcurl 通过 c 程序从 owlbot 获取含义

Using libcurl to get meaning from owlbot via a c program

我想使用 libcurl 编写一个 c 程序,它等效于 Unix 终端上的此命令: curl --header "Authorization: Token <I placed the key over here without these "<>">" https://owlbot.info/api/v4/dictionary/owl -s | json_pp

下面是我的代码是使用其他帖子并阅读库的文档编写的。 但是我从服务器收到如下错误: {"detail": "Authentication credentials were not provided."}

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

int main(){
  //initializes some of libcurl functionality globally
  curl_global_init(CURL_GLOBAL_ALL);
  
  CURLcode res;
  /*creates an easy handle for using the easy interface.
   *A handle is a logic entity for the upcoming transfer or series of transfer */
  CURL *easyhandle;
  easyhandle = curl_easy_init();

  struct curl_slist *headers = NULL;
  curl_slist_append(headers,"Authorization: Token <I placed the key over here without these "<>">");
  curl_slist_append(headers,"Content-Type:text/json");
    //Pass the list of custom made headers
  curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers);
 curl_easy_setopt(easyhandle,CURLOPT_URL,"https://owlbot.info/api/v4/dictionary/owl");

  
 
  res = curl_easy_perform(easyhandle);
  curl_easy_cleanup(easyhandle);
  curl_global_cleanup();
  
}

创建自定义 headers 的代码需要正确传递 headers 变量,因为 curl_slist_append returns 新指针.

该小节的固定版本可能与此类似:

  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "Authorization: Token <I placed the key over here without these "<>">");
  headers = curl_slist_append(headers, "Content-Type:text/json");
  curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers);