Restweb icon indicating copy to clipboard operation
Restweb copied to clipboard

Different URLs call different get requests

Open Alanscut opened this issue 4 years ago • 5 comments

Is your feature request related to a problem? Please describe. Thank you very much for your project, it helps me a lot since I'm a green hander for C++. According to your demo, I could easily create a server which has 4 different requests, and we visit them through the same url http://localhost:34568 with different request types(GET, POST ..).

But if I want to create another GET request like http://localhost:34568/rest/api, I have no idea how to do it.

Describe the solution you'd like if you don't mind, please add an example code in this project.

Additional context what I want to do is that: when visit http://localhost:34568/first, then excute handle_get1, and when visit http://localhost:34568/second, then excute handle_get2. handler.cpp

// visit it by url "http://localhost:34568/first"
void handler::handle_get1(http_request message) {
  // do some things
}

// visit it by url "http://localhost:34568/seconde"
void handler::handle_get2(http_request message) {
  // do other things
}

Alanscut avatar Feb 26 '20 07:02 Alanscut

Hi @Alanscut ,

Thank you for highlighting the feature, it's a good point so I'll add the demo for same .

Meenapintu avatar Feb 28 '20 14:02 Meenapintu

Thanks, that's great! Could you please offer a simple demo here 😄 ?

Alanscut avatar Feb 29 '20 01:02 Alanscut

Hi @Alanscut Uploading the actual demo code will take some time as I'm stuck in something else but let me put the logic of implementation here with pseudo code to help you out.

I was looking at the code , http_listener listener(baseURI) only supports the single url so best approach would be writing a router middleware that will route the requests according to paths.

So eventually the current handler will act as a router that will redirect the requests to the specified handler.

pseudo logic for same would be and where . suppose you want to route 2 get requests as you mentioned is comment. As all the get requests will come to the void handler::handle_get(http_request message) method. so let's modify it.


void handler::handle_get(http_request message){

}.  // current this code will act as ` void route(http_request message);` of myRouter class below.

==================myRouter.h====
class myRouter{
      private: HashContainer<url,handler> routingData;
      ...
   public :

      void registerHandler(string path_OR_pathRegex, myHandler handler); //this will register the paths with handler , you can call it multiple times so register multiple paths. 
      void  registerDefaultHandler( myHandler handler); //register for undefined paths. 
      void route(http_request message); //this will route to specific handler registered by  `registerHandler` method for a path. 
      ...

}

class myHandler{
      void handle_get1(http_request message);
      void handle_get2(http_request message);
}
// now register your logic handler like this.
myRouter.register("/seconde",&myhandler::handle_get2);
myRouter.register("/first",&myhandler::handle_get1);



Also this can be extend to domain name based routing.

ActionItems:

  1. I've to do some research that, Is there exists such router library implementation or have to implement.

Meenapintu avatar Feb 29 '20 08:02 Meenapintu

Dear Meena, thanks for your patience and friendly instructions, I'll try it later. 👍

Alanscut avatar Feb 29 '20 08:02 Alanscut

Hi @Alanscut ,

Thanks , I've noted the action Item and will complete this asap but If you implement the router in generic way or find any good library to do such job in the mean time, please let me know and feel free to ask for any help. I'm always ready to help and learn.

Thanks, All the best.

Meenapintu avatar Feb 29 '20 17:02 Meenapintu