我试图使用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];
}];
我承认我太仔细研究Swift/Objective-C了,以至于忽略了HTML中一个愚蠢的东西。我愚蠢地写了……
value='name'
name='value'
欢呼。