C++、JsonCpp、libcurl 和 UTF-8 问题

发布于 2024-09-19 10:18:52 字数 1622 浏览 5 评论 0原文

我在使用 libcurl 与 C++ JsonCpp 库一起工作时遇到了一些问题,经过大量研究,我想出了这段代码:

int post(const string& call, const string& key, const string& value) {
  // (...)

  char* char_data=NULL; 
  struct curl_slist *headers=NULL;

  headers = curl_slist_append(headers, "Content-Type: application/json; charset=UTF-8");

  Json::Value root;
  root[key] = value;

  Json::StyledWriter writer;
  string data = writer.write(root);

  char_data = (char*) malloc((strlen(data.c_str())+1) * sizeof(char));
  strcpy(char_data, data.c_str());

  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, char_data);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(char_data));

  // (...)
}

只要 data (C++ std:: string 保存 JSON 字符串表示形式)没有任何非 ASCII 字符。当它发生时,我从后端收到一个错误(用 Rails 3 编写):

Started POST "/deployments/4c904f607d7c4249cf00002c/log.json" for 67.23.79.89 at Wed Sep 15 00:45:40 -0400 2010
  Processing by DeploymentsController#log as JSON
  Parameters: {"log"=>"0 upgraded, 0 newly \214\211K########talled, 0 to remove and 46 not upgraded.\n", "id"=>"4c904f607d7c4249cf00002c"}
Completed   in 6ms

BSON::InvalidStringEncoding (String not valid UTF-8):
  app/models/deployment.rb:161:in `log'
  app/models/deployment.rb:160:in `each'
  app/models/deployment.rb:160:in `log'
  app/controllers/deployments_controller.rb:54:in `log'

What is the best way to take a C++ sstring (in this case data), and secure it conversion it UTF-8, and then to a *char 变量可以与 libcurl 配合使用吗?

I had some problems making libcurl work with C++ JsonCpp library and, after a lot of research, I came up with this code:

int post(const string& call, const string& key, const string& value) {
  // (...)

  char* char_data=NULL; 
  struct curl_slist *headers=NULL;

  headers = curl_slist_append(headers, "Content-Type: application/json; charset=UTF-8");

  Json::Value root;
  root[key] = value;

  Json::StyledWriter writer;
  string data = writer.write(root);

  char_data = (char*) malloc((strlen(data.c_str())+1) * sizeof(char));
  strcpy(char_data, data.c_str());

  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, char_data);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(char_data));

  // (...)
}

This works fine, as long as the data (C++ std::string that holds the JSON string representation) doesn't have any non-ascii chars. When it does, I get an error from the backend (written in Rails 3):

Started POST "/deployments/4c904f607d7c4249cf00002c/log.json" for 67.23.79.89 at Wed Sep 15 00:45:40 -0400 2010
  Processing by DeploymentsController#log as JSON
  Parameters: {"log"=>"0 upgraded, 0 newly \214\211K########talled, 0 to remove and 46 not upgraded.\n", "id"=>"4c904f607d7c4249cf00002c"}
Completed   in 6ms

BSON::InvalidStringEncoding (String not valid UTF-8):
  app/models/deployment.rb:161:in `log'
  app/models/deployment.rb:160:in `each'
  app/models/deployment.rb:160:in `log'
  app/controllers/deployments_controller.rb:54:in `log'

What is the best way to take a C++ sctring (in this case data), and safely convert it UTF-8, and then to a *char variable that would play nice with libcurl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-09-26 10:18:52

我发现了问题。它不在代码的那部分。我实际上是在进行字符串拆分,导致了问题。

I found the problem. It wasn't on that part of code. I was actually doing a string split that was causing the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文