表单请求不包含任何键

时间:2015-09-28 18:47:03

标签: swift gcdwebserver

我尝试使用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 Version

[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];
}];

1 个答案:

答案 0 :(得分:0)

槽糕。我承认我正在密切关注Swift / Objective-C,我在HTML中错过了一个愚蠢的事情。我愚蠢地写了......

value='name' 

应该是......

name='value'

干杯。