C++、JsonCpp、libcurl 和 UTF-8 问题
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题。它不在代码的那部分。我实际上是在进行字符串拆分,导致了问题。
I found the problem. It wasn't on that part of code. I was actually doing a string split that was causing the problem.