I am new to using http request and cpp programming so do not hesitate to report any approximations about what I am saying. I have a python server that runs in local at the address "http://127.0.0.1:8000/info" where if I open my navigator at this address, I display "Hello World". I would like to make a cpp server that can get this "Hello World" string from this address (or any messages I would write instead) and show it back at another address "http://127.0.0.1:8080". I have the following code that compiles using oatpp to generate my server :
#include <iostream>
#include "oatpp/core/Types.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
#include "oatpp/network/ConnectionProvider.hpp"
#include "oatpp/network/Server.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include OATPP_CODEGEN_BEGIN(ApiController) // Beginning of code generation for API controller
class MyController : public oatpp::web::server::api::ApiController {
public:
MyController(const std::shared_ptr<ObjectMapper>& objectMapper)
: oatpp::web::server::api::ApiController(objectMapper) {}
ENDPOINT("GET", "/", root) {
auto resp = "Hello, World!";
auto response = createResponse(Status::CODE_200, resp);
return response;
}
};
#include OATPP_CODEGEN_END(ApiController) // End of code generation for API controller
void run() {
oatpp::base::Environment env;
// Create a HTTP connexion provider
auto connectionProvider = oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", 8080});
auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
// Create the HTTP router for server requests
auto router = oatpp::web::server::HttpRouter::createShared();
// Add controller to the router
router->addController(std::make_shared<MyController>(objectMapper));
// Create a handler for HTTP connexion
auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);
// Create HTTP server
oatpp::network::Server server(connectionProvider, connectionHandler);
// Start server
std::cout << "Server started on port 8080" << std::endl;
server.run();
}
int main() {
run();
return 0;
}
Now, I am trying to code a GET request from my first python server and cout the resulting string from this request, but I cannot find a way to make it work. This is my current code :
#include "oatpp/web/client/HttpRequestExecutor.hpp"
#include "oatpp/network/tcp/client/ConnectionProvider.hpp"
// Make a http request to get data
auto connectionProvider = oatpp::network::tcp::client::ConnectionProvider::createShared({"http://127.0.0.1:8000/platform/battery/getInfoBattery", 8000});
auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(connectionProvider);
auto headers = oatpp::web::protocol::http::Protocol::Headers::createShared();
headers->put("user-agent", "super-agent");
auto response = requestExecutor->executeOnce("GET", "/endpoint", headers, nullptr);
std::cout << response->readBodyToString->c_str() << std::endl;
This code is based on the github of oatpp about HTTP GET request but needs to be updated as I am using oatpp-1.3.0 where oatpp::web::protocol::http::Protocol::Headers::createShared() does not exist anymore (it might be deprecated). Do you know how can I make it work ?
Thanks a lot for your help.