curl使用C ++发送JSON数据不起作用

时间:2018-04-28 01:20:44

标签: c++ curl

我需要使用cUrl库发送ID(JSON数据)并且它无法正常工作。

这是命令:

curl --request POST \
  --url https://pos-api.ifood.com.br/v1.0/events/acknowledgment \
  --header 'Authorization: bearer {acess_token}' \
  --header 'Cache-Control: no-cache' \
  --header 'Content-Type: application/json' \
  --data '[{"id":"Event_id1"},{"id":"Event_id2"},{"id":"Event_id3"},{"id":"Event_id4"}]'

我有这个功能。输入参数是: 有效令牌和str(JSON数据)为[{"id":"b7e17af5-fd21-4690-b908-71d489d53355"}]

使用此JSON输入数据和有效令牌在命令行中没有错误,但是当我尝试使用我的C ++函数时,会出现错误400(最后显示)。

int iFood_Acknowledgment(QString str, QString token) {
    CURL *curl;
    CURLcode res;
    QString autorizacao;

    autorizacao=QString("Authorization: bearer %1").arg(token);

      /* In windows, this will init the winsock stuff */
      curl_global_init(CURL_GLOBAL_ALL);

      /* get a curl handle */
      curl = curl_easy_init();
      if(curl) {
          qDebug() << "autorizacao " << autorizacao.toLocal8Bit().constData() << autorizacao.toUtf8();
          qDebug() << "iFood_Acknowledgment executando..." << qPrintable(str);

        /* First set the URL that is about to receive our POST. This URL can
           just as well be a https:// URL if that is what should receive the
           data. */
          struct curl_slist *headers = NULL;

          curl_easy_setopt(curl, CURLOPT_URL, "https://pos-api.ifood.com.br/v1.0/events/acknowledgment");
          curl_easy_setopt(curl, CURLOPT_POSTFIELDS, qPrintable(str));
          curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, str.size());
          curl_easy_setopt(curl, CURLOPT_POST, 1L);
   //       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");

          headers = curl_slist_append(headers, qPrintable(autorizacao));
          headers = curl_slist_append(headers, "Cache-Control: no-cache");
          headers = curl_slist_append(headers, "Content-Type: application/json;charset=UTF-8");
       //   headers = curl_slist_append(headers, "charsets: utf-8");

          curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

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

        /* always cleanup */
        curl_easy_cleanup(curl);
        curl_global_cleanup();
        curl_slist_free_all(headers);

        return 1;

      }
      curl_global_cleanup();
      return 0;
}

此函数返回错误代码400:

{"timestamp":"2018-04-28T01:10:32.681+0000","status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Bad Request","path":"/events/acknowledgment"}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

谢谢大家。

我正在使用Qt(它解释了QString和qDebug)。

问题是从qPrintable到(char *)

的输出

在qDebug中向我显示的qPrintable输出似乎没问题,但不是!

我说这个:         char * s;

    s=(char *)malloc(str.length());
    QByteArray ba=str.toLatin1();  // str is my json input data
    strcpy(s,ba.data());

并且

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, s);

现在这个功能还可以。

谢谢大家!