Search code examples
htmliosobjective-cformsgcdwebserver

Differentiate form actions in GCDWebServer?


I am using two forms on an HTML page hosted via GCDWebServer. I have the first form setup like this...

<form name=\"vendorInput\" method=\"post\" enctype=\"application/x-www-form-urlencoded\">
<input type=submit value='Add Vendor' action=\"/\">

and the second form setup like this...

<form name=\"vendorInput\" method=\"post\" enctype=\"application/x-www-form-urlencoded\">
<input type=submit value='Add Item' action=\"/\">

I can't find any documentation that provides support for this; and any action string I type other than / causes the HTML request to break. Is there a way to parse different actions for form submit buttons in GCDWebServer?


Solution

  • You just need to have the action be a different path for each form and then implement a GCDWebServer handler for each path:

    [webServer addHandlerForMethod:@"POST"
                              path:@"/path1"
                      requestClass:[GCDWebServerURLEncodedFormRequest class]
                      processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
    
      // Do something with form data i.e. [[(GCDWebServerURLEncodedFormRequest*)request arguments]
      return [GCDWebServerDataResponse responseWithHTML:@"<html><body>OK</body></html>"];
    
    }];
    
    [webServer addHandlerForMethod:@"POST"
                              path:@"/path2"
                      requestClass:[GCDWebServerURLEncodedFormRequest class]
                      processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
    
      // Do something with form data i.e. [[(GCDWebServerURLEncodedFormRequest*)request arguments]
      return [GCDWebServerDataResponse responseWithHTML:@"<html><body>OK</body></html>"];
    
    }];
    

    See https://github.com/swisspol/GCDWebServer#advanced-example-2-implementing-forms for an example.