Closed Marcel1707 closed 4 years ago
Hello @Marcel1707 ,
Thanks for your question!
You can easily do it by doing the same steps as in the Connector::connect() - just add headers that you need to the map.
It might be also a good idea to extend Connector by adding a connect method with headers...
Regards, Leonid
Hey,
I tried it with the following code but it still did not work. Do I have to replace the connect method completely?
oatpp::base::Environment::init();
std::cout << "application started" << std::endl;
auto config = oatpp::mbedtls::Config::createDefaultClientConfigShared();
auto connectionProvider = oatpp::mbedtls::client::ConnectionProvider::createShared(config, "192.168.1.106", 5000);
auto connectionHandler = oatpp::websocket::ConnectionHandler::createShared();
auto connector = oatpp::websocket::Connector::createShared(connectionProvider);
try {
oatpp::websocket::Handshaker::Headers headers;
headers.put("Test", "Test");
oatpp::websocket::Handshaker::clientsideHandshake(headers);
auto connection = connector->connect("/");
std::cout << "connected" << std::endl;
auto socket = oatpp::websocket::WebSocket::createShared(connection, true);
std::mutex socketWriteMutex;
socket->setListener(std::make_shared<WSListener>(socketWriteMutex));
std::cout << "listening ..." << std::endl;
socket->listen();
}
catch (const std::exception& e) {
std::cout << "connection error: " << e.what() << std::endl;
}
Regards, Marcel
Hey,
Yes, you have to replace the Connect method. You need to call execute
method with your headers provided (like inside the connect method)
I'll provide the code snippet later today
Hey @Marcel1707 , did you manage to make it work?
[TODO] Add possibility to pass headers to the Connector::connect()
method.
No I did some other things in the meantime hoping that you provide the code snippet :)
I came to another question: What is the easiest way to execute just a http request? Without using the APIClient..
Thanks and kind regards, Marcel
Hey,
No I did some other things in the meantime hoping that you provide the code snippet :)
I'll make a patch to oatpp-websocket
tonight to make it possible to pass headers.
And will put a code snippet here.
What is the easiest way to execute just a http request? Without using the APIClient..
The easiest way is to use HttpRequestExecutor
directly - please find this answer
Regards, Leonid
Ah great thanks :)
I am currently trying to make the request with HttpRequestExecutor but I cant manage to add a request body in JSON format. I tried to use a BufferBody with the JSON string and text/json as content type but on the server the body is always empty.
Kind regards, Marcel
Hey,
I tried to use a BufferBody with the JSON string and text/json as content type but on the server the body is always empty.
It sounds correct except that the content-type header should be application/json
.
Please provide your code snippet.
Regards, Leonid
Oh thanks, with 'application/json' it works 👍
Hey @Marcel1707 ,
I've made a patch to pass headers in the WS connect request. Please checkout the latest oatpp-websocker
master branch.
oatpp::web::protocol::http::Headers headers;
headers.put("X-MyHeader", "MyValue");
auto connection = connector->connect("/", headers /* pass headers here */);
Regards, Leonid
I'm closing this issue. @Marcel1707 marcel please feel free to reopen or to create the new one in case it doesn't work for you.
Hello, I am using oatpp-websocket for a client connection and I need to pass some request headers at the initial connection. Is there a possibility to add request headers (like a token) to the connection?