Nsurlrequest [get / post]到gcdwebserver urlQuery null

时间:2016-05-20 12:48:25

标签: ios gcdwebserver

我正在研究gcdwebserver,我发送邮件请求到gcdwebserver,也发送了一些参数,但是在addhandlermatchblock中没有得到查询参数,请回复

代码:

    NSString * postString=@"name=ram&age=20";
   NSURLSession * session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURL * url=[NSURL URLWithString:@"http://127.0.0.1:8080/index.html"];
    NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 


[webServer addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) {

        if (![requestMethod isEqualToString:@"GET"]) {
            return nil;
        }


        return [[GCDWebServerRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];

    } processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
            NSLog(@"coming here get %@",request.urlQuery); // null
        GCDWebServerResponse* response = nil;

        return response;

                              }];

1 个答案:

答案 0 :(得分:0)

我尝试使用GCDWebServerURLEncodedFormRequest代替GCDWebServerRequest,但它有效

  [webServer addHandlerWithMatchBlock:^GCDWebServerURLEncodedFormRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) {

            if (![requestMethod isEqualToString:@"GET"])
            {
                return [[GCDWebServerURLEncodedFormRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
            }
            else
            {
                return [[GCDWebServerURLEncodedFormRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
            }

        } processBlock:^GCDWebServerResponse *(GCDWebServerURLEncodedFormRequest * request) {

 NSMutableDictionary * dic=[[NSMutableDictionary alloc]initWithDictionary:[request arguments]]; // url query params


    return response;
    }