我正在尝试使用 Swift 来处理一个简单的 HTML 表单。按照 READ ME 中给出的 Objective-C 代码,我想出了下面的 Swift。
let webServer = GCDWebServer()
webServer.addDefaultHandlerForMethod("GET", requestClass: GCDWebServerRequest.self, processBlock: {request in
return GCDWebServerDataResponse(HTML:"<html><head><title>Hello</title></head><body><form method='post' action='/' enctype='application/x-www-form-urlencoded'><input type='text' value='name'><input type='submit' value='Submit'> </form></body></html>")
})
webServer.addHandlerForMethod("POST", path: "/", requestClass: GCDWebServerURLEncodedFormRequest.self, asyncProcessBlock: {request in
let value = request.0 as! GCDWebServerURLEncodedFormRequest
//value has no keys
})
我肯定错过了什么。
Objective-C 版本
[webServer addHandlerForMethod:@"POST"
path:@"/"
requestClass:[GCDWebServerURLEncodedFormRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
NSString* value = [[(GCDWebServerURLEncodedFormRequest*)request arguments] objectForKey:@"value"];
NSString* html = [NSString stringWithFormat:@"<html><body><p>%@</p></body></html>", value];
return [GCDWebServerDataResponse responseWithHTML:html];
}];