drogonframework / drogonframework/drogon
client->sendRequest problem when offering Http service at the same time
- Dominant language
- C++
- Stars
- 14.3k
- Forks
- 1.4k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 14
Description
When I launch a client to login a backend management system while a http service opened, the client-sendRequst() would not run. I tried for several methods include sync and asyn, it would not work.
the code is like this:
if(!logConfig.isNull()) {
trantor::Logger::setLogLevel(trantor::Logger::kInfo);
DSLog("Log level set from config");
}
else {
auto logger = std::make_shared();
logger->setFileName("./logs/drogon_app.log");
logger->startLogging();
trantor::Logger::setOutputFunction(
[logger](const char* msg, const uint64_t len)
{
logger->output(msg, len);
},
[logger]()
{
logger->flush();
}
);
trantor::Logger::setLogLevel(trantor::Logger::kInfo);
DSLog("Log file configured");
}
std::string url = g_dimsConfig.serverIp + ":" + g_dimsConfig.serverPort;
DSLog(("Server URL: " + url).c_str());
auto client = drogon::HttpClient::newHttpClient(url);
Json::Value loginData;
loginData["username"] = g_dimsConfig.loginUsername;
loginData["password"] = g_dimsConfig.loginPassword;
loginData["user_type"] = g_dimsConfig.user_type;
loginData["captcha"] = g_dimsConfig.captcha;
loginData["captchaKey"] = g_dimsConfig.captchaKey;
Json::FastWriter writer;
std::string jsonBody = writer.write(loginData); // 显式序列化为字符串
DSLog(("Request Body: " + jsonBody).c_str());
auto req = drogon::HttpRequest::newHttpJsonRequest("{\"captcha\":\"\",\"captchaKey\":\"\",\"password\":\"a66abb5684c45962d887564f08346e8d\",\"user_type\":\"\",\"username\":\"superadmin\"}");
req->setMethod(drogon::Post);
req->setPath("/api/login");
//req->setContentTypeString("application/json");
req->setBody(jsonBody); // 手动设置请求体
drogon::app().setThreadNum(1); // 初始化至少一个线程
DSLog(("Request Path: " + g_dimsConfig.loginPath).c_str());
DSLog(("Request Body: " + loginData.toStyledString()).c_str());
DSLog("Sending synchronous request");
drogon::ReqResult result;
drogon::HttpResponsePtr response;
try {
DSLog("Before sendRequest");
std::tie(result, response) = client->sendRequest(req, 0.5); // 缩短超时便于调试
DSLog("Request completed");
if(result == drogon::ReqResult::Ok && response) {
DSLog("Response received");
DSLog(response->body().data());
/*if(response->getJsonObject() && response->getJsonObject()->isMember("data")) {
g_authToken = std::string("JWT ") + (*response->getJsonObject())["data"]["access"].asString();
LOG_INFO << "Login successful, token: " << g_authToken;
}*/
}
else {
DSLog("Request failed");
if(response) {
DSLog(("Status: " + std::to_string(response->statusCode())).c_str());
DSLog(response->body().data());
}
LOG_ERROR << "Request failed with result: " << (int)result;
}
}
catch(const std::exception& e) {
DSLog(("Send request error: " + std::string(e.what())).c_str());
return 1;
}
I'm sure the backend servise is OK and request from Apifox, it's OK to get right response. But this code not.
**Desktop (please complete the following information):**
- OS: Windows11
- Browser chorme, but not used
- Version [e.g. 22]
could any body give me some advices? Thank you.
Contributor guide
Assessment
This issue has not been assessed yet.