如何将向量发布到HTTP端点

How to post vector to a HTTP endpoint

本文关键字:HTTP 端点 向量      更新时间:2023-10-16

i具有以下数据类型,该数据类型在C

中填充
std::vector<uint8_t> bytes;

使用libcurl,如何将其发布到HTTP端点?

尝试了以下代码,但我认为它与我的帖子数据不起作用

CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "http://1.2.3.4:9002/multicastdataclient-message");
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &bytes);
  res = curl_easy_perform(curl);
  if(res != CURLE_OK)
    fprintf(stderr, "curl_easy_perform() failed: %sn",
            curl_easy_strerror(res));
  curl_easy_cleanup(curl);
}

基于所有注释,以下代码有效!

卷发 *卷发; 夹层res;

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "http://1.2.3.4:9002/message");
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, bytes.size());
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, bytes.data());
  res = curl_easy_perform(curl);
  if(res != CURLE_OK)
    fprintf(stderr, "curl_easy_perform() failed: %sn",
  curl_easy_strerror(res));
  curl_easy_cleanup(curl);
}
else
{
  DBG("curl empty !!");
}