parker parker - 6 months ago 53
iOS Question

Nsurlrequest [get/post] to gcdwebserver urlQuery null

I am working on gcdwebserver,i am sending post request to gcdwebserver,sending some params also,but in addhandlermatchblock not getting query params,please reply

code:

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;

}];

Answer

I tried with GCDWebServerURLEncodedFormRequest in place of GCDWebServerRequest ,it worked

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