c++ - C++ 不能将 ExpressJS API 检索到的 GET 请求中的响应字符串用于打印以外的任何内容
问题描述
场景:我正在编写一个 c++ 程序,它应该从 expressJS API 中检索文件和字符串。使用 curlRequests 下载 txt.files 效果很好,但是一旦我尝试检索纯字符串,它们就只能用于打印。
问题:当尝试对来自我的 GET 请求(来自 expressjs api)的响应执行任何操作时,我的响应不会被视为字符串。
string myText = curlRequest.GetText("/templates/names");
string myTextB = "react.txt, scss.txt"
cout << myText << endl; // prints"react.txt, scss.txt"
cout << myTextB << endl; // prints "react.txt, scss.txt"
cout << stringHelper.GetSubstringPos(myText, "scss") << endl; // printsstring::npos
cout << stringHelper.GetSubstringPos(myTextB, "scss") << endl; // printsposition of 's' as expected
这是我在 c++ 中 curlrequest.h 的 GetText 函数
string GetText(string ACTIONURL) {
CURL* curl;
CURLcode curlRes;
string res;
string url = templateCreator.APIURL + ACTIONURL;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curlRes = curl_easy_perform(curl);
res = curlRes;
if (curlRes == CURLE_HTTP_RETURNED_ERROR) {
res = "FAILED";
}
else if (curlRes != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(curlRes));
res = "FAILED";
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return res;
}
这是expressjs中的路由(接收请求并用字符串响应)
router.get('/names', function(req, res, next) {
var str = "react.txt, scss.txt, example.txt";
res.send(str);
});
// I also tried sending a buffer but as its sended as string i face the same problem..
// C++ could decode the buffer ..
router.get('/buf', function(req, res, next) {
let data = 'hello world: (1 + 2 !== 4)';
let buff = new Buffer.from(data);
let base64data = buff.toString('base64');
console.log(base64data);
res.send(base64data);
});
从我的 expressjs API 中检索文本文件不是问题..
void GetFile(string ACTIONURL, string OUTDIR) {
CURL* curl;
FILE* fp;
CURLcode res;
string url = templateCreator.APIURL + ACTIONURL;
curl = curl_easy_init();
if (curl)
{
fopen_s(&fp, OUTDIR.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return;
}
(下载后我可以逐行阅读这些内容并存储在矢量中)。
我仍然希望能够发送实际的字符串。有没有人知道为什么我在这里遇到问题?我确实已经在这个意想不到的“小”问题上花了几天时间......
解决方案
谢谢@n.'pronouns'm。我更新了我的功能,并意识到这res = curlRes
是一个明智的选择。而且对有效响应的检查现在也没有了。
//those 2 lines and a write_to_string function were missing and `res = curlRes` should do their job
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
以下 2 个函数现在替换了我的 GetText 函数,一切都按预期工作。
size_t write_to_string(void* ptr, size_t size, size_t count, void* stream) {
((string*)stream)->append((char*)ptr, 0, size * count);
return size * count;
}
string GetText(string ACTIONURL) {
CURL* curl;
CURLcode res;
string response;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, templateCreator.APIURL + ACTIONURL.c_str());
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
return response;
}
谢谢!我之前也找到了 1 或 2 个问题的解决方法,但不知道这是实际问题。现在可以使用字符串了!
推荐阅读
- java - 火花蓄能器
- julia - Julia函数返回数组的非唯一元素
- javascript - 如果 package-lock.json 锁定它,那么在 package.json 中声明“兼容版本”(^version)有什么意义?
- apache-kafka - 主题中的kafka搜索文本
- soap - 在 SoapUI 中执行 SOAP 请求后执行 Groovy 脚本
- localization - 本地化应用程序时的共轭词
- python - 有什么方法可以通过 python 为 nao 机器人创建映射?
- javascript - 在不使用工厂的情况下使用 AngularJS、ES6 和 Typescript 实例化新模型
- ruby-on-rails - 如何自定义 Gitlab LDAP 认证功能?
- angular - Angular - 每个客户的远程 API 的基本 URL 映射