oatpp / oatpp-websocket

oatpp-websocket submodule.
https://oatpp.io/
Apache License 2.0
83 stars 32 forks source link

Request headers #20

Closed Marcel1707 closed 4 years ago

Marcel1707 commented 4 years ago

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?

lganzzzo commented 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

Marcel1707 commented 4 years ago

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

lganzzzo commented 4 years ago

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

lganzzzo commented 4 years ago

Hey @Marcel1707 , did you manage to make it work?

lganzzzo commented 4 years ago

[TODO] Add possibility to pass headers to the Connector::connect() method.

Marcel1707 commented 4 years ago

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

lganzzzo commented 4 years ago

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

Marcel1707 commented 4 years ago

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

lganzzzo commented 4 years ago

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

Marcel1707 commented 4 years ago

Oh thanks, with 'application/json' it works 👍

lganzzzo commented 4 years ago

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

lganzzzo commented 4 years ago

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.