Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should I make my process send requests to the target when using OATPP #925

Open
ZzMLvzZ-792998470 opened this issue Mar 13, 2024 · 4 comments

Comments

@ZzMLvzZ-792998470
Copy link

I am currently using C++to create a video engine, and I can use the OATPP framework to receive external requests and execute corresponding methods. But now I hope to send a post request to the backend at the specified address when executing some of my methods. I tried to encapsulate a class but it doesn't seem to work. Can you help me solve my problem? Thank you very much!

``
class RequestHandler {
public:
RequestHandler() {}

~RequestHandler() {}

// 注册地址和端口
void registerConnection(const std::string& url = "http://127.0.0.1", int port = 9002) {
    _connectionProvider =  oatpp::network::tcp::client::ConnectionProvider
            ::createShared({url, static_cast<v_uint16>(port)});
    _requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(_connectionProvider);
}
// 解绑
void unregisterConnection() {
    _connectionProvider->stop();
    _connectionProvider = nullptr;
    _requestExecutor = nullptr;
}
// 发送请求并返回response
std::shared_ptr<oatpp::web::protocol::http::incoming::Response> sendRequest(const std::string& order, const std::string& endpoint, const std::string& message) {
    auto headers = oatpp::web::protocol::http::Headers();
    headers.put("user-agent", "super-agent");

    if(!_requestExecutor) {
        std::cerr << "_requestExecutor is not exist" << std::endl;
        return nullptr;
    }

    auto response = _requestExecutor->executeOnce(order, endpoint, headers, nullptr);
    return response;
}

private:
std::shared_ptroatpp::network::tcp::client::ConnectionProvider _connectionProvider;
std::shared_ptroatpp::web::client::HttpRequestExecutor _requestExecutor;
};
``

my error:
zhaozimo@zhaozimodeMacBook-Pro test % sh livePlatform_test_request.sh
server=oatpp/1.3.0
code=500
description=Internal Server Error
message=[oatpp::web::client::HttpRequestExecutor::executeOnce()]: Connection is null

@doufu3344
Copy link
Contributor

Try pass _requestExecutor->getConnection() as the last parameter of _requestExecutor->executeOnce()

You'd better refer to the documentation and implement your own apiclient to send a request.

@ZzMLvzZ-792998470
Copy link
Author

多谢老哥 我对这个框架还不是很熟 剩下的东西我看看文档自己解决吧

image

@doufu3344
Copy link
Contributor

_connectionProvider = oatpp::network::tcp::client::ConnectionProvider
::createShared({url, static_cast<v_uint16>(port)});

The url is incorrect, this parameter represents the host address, so it should be 127.0.0.1 not http://127.0.0.1

@ZzMLvzZ-792998470
Copy link
Author

牛的老哥 我搞定了! 十分感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants