From 8bdbf7ca78aa662291cc1e586204be9add7e636d Mon Sep 17 00:00:00 2001 From: Sefa Ilkimen Date: Wed, 3 Apr 2019 14:28:12 +0200 Subject: [PATCH] fix query params handling for iOS --- src/ios/CordovaHttpPlugin.m | 208 +++++++++++++++++------------------- 1 file changed, 101 insertions(+), 107 deletions(-) diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index b89bc1e..ce39a6c 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -161,16 +161,14 @@ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } -- (void)post:(CDVInvokedUrlCommand*)command { +- (void)get:(CDVInvokedUrlCommand*)command { manager.securityPolicy = securityPolicy; NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; - NSString *serializerName = [command.arguments objectAtIndex:2]; - NSDictionary *headers = [command.arguments objectAtIndex:3]; - NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue]; + NSDictionary *headers = [command.arguments objectAtIndex:1]; + NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:2] doubleValue]; - [self setRequestSerializer: serializerName forManager: manager]; + [self setRequestSerializer: @"default" forManager: manager]; [self setRequestHeaders: headers forManager: manager]; [self setTimeout:timeoutInSeconds forManager:manager]; [self setRedirect: manager]; @@ -180,7 +178,7 @@ [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; @try { - [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { + [manager GET:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; @@ -202,14 +200,51 @@ } } -- (void)get:(CDVInvokedUrlCommand*)command { +- (void)head:(CDVInvokedUrlCommand*)command { manager.securityPolicy = securityPolicy; NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; - NSDictionary *headers = [command.arguments objectAtIndex:2]; - NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:3] doubleValue]; + NSDictionary *headers = [command.arguments objectAtIndex:1]; + NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:2] doubleValue]; + [self setRequestHeaders: headers forManager: manager]; + [self setTimeout:timeoutInSeconds forManager:manager]; + [self setRedirect: manager]; + + CordovaHttpPlugin* __weak weakSelf = self; + manager.responseSerializer = [AFHTTPResponseSerializer serializer]; + [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; + + @try { + [manager HEAD:url parameters:nil success:^(NSURLSessionTask *task) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + // no 'body' for HEAD request, omitting 'data' + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:nil]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; + }]; + } + @catch (NSException *exception) { + [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; + [self handleException:exception withCommand:command]; + } +} + +- (void)delete:(CDVInvokedUrlCommand*)command { + manager.securityPolicy = securityPolicy; + + NSString *url = [command.arguments objectAtIndex:0]; + NSDictionary *headers = [command.arguments objectAtIndex:1]; + NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:2] doubleValue]; [self setRequestSerializer: @"default" forManager: manager]; [self setRequestHeaders: headers forManager: manager]; @@ -221,7 +256,48 @@ [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; @try { - [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { + [manager DELETE:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; + }]; + } + @catch (NSException *exception) { + [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; + [self handleException:exception withCommand:command]; + } +} + +- (void)post:(CDVInvokedUrlCommand*)command { + manager.securityPolicy = securityPolicy; + + NSString *url = [command.arguments objectAtIndex:0]; + NSDictionary *data = [command.arguments objectAtIndex:1]; + NSString *serializerName = [command.arguments objectAtIndex:2]; + NSDictionary *headers = [command.arguments objectAtIndex:3]; + NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue]; + + [self setRequestSerializer: serializerName forManager: manager]; + [self setRequestHeaders: headers forManager: manager]; + [self setTimeout:timeoutInSeconds forManager:manager]; + [self setRedirect: manager]; + + CordovaHttpPlugin* __weak weakSelf = self; + manager.responseSerializer = [TextResponseSerializer serializer]; + [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; + + @try { + [manager POST:url parameters:data success:^(NSURLSessionTask *task, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; @@ -247,7 +323,7 @@ manager.securityPolicy = securityPolicy; NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; + NSDictionary *data = [command.arguments objectAtIndex:1]; NSString *serializerName = [command.arguments objectAtIndex:2]; NSDictionary *headers = [command.arguments objectAtIndex:3]; NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue]; @@ -262,7 +338,7 @@ [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; @try { - [manager PUT:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { + [manager PUT:url parameters:data success:^(NSURLSessionTask *task, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; @@ -288,7 +364,7 @@ manager.securityPolicy = securityPolicy; NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; + NSDictionary *data = [command.arguments objectAtIndex:1]; NSString *serializerName = [command.arguments objectAtIndex:2]; NSDictionary *headers = [command.arguments objectAtIndex:3]; NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue]; @@ -303,7 +379,7 @@ [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; @try { - [manager PATCH:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { + [manager PATCH:url parameters:data success:^(NSURLSessionTask *task, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; @@ -325,94 +401,14 @@ } } -- (void)delete:(CDVInvokedUrlCommand*)command { - manager.securityPolicy = securityPolicy; - - NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; - NSDictionary *headers = [command.arguments objectAtIndex:2]; - NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:3] doubleValue]; - - [self setRequestSerializer: @"default" forManager: manager]; - [self setRequestHeaders: headers forManager: manager]; - [self setTimeout:timeoutInSeconds forManager:manager]; - [self setRedirect: manager]; - - CordovaHttpPlugin* __weak weakSelf = self; - manager.responseSerializer = [TextResponseSerializer serializer]; - [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; - - @try { - [manager DELETE:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; - }]; - } - @catch (NSException *exception) { - [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; - [self handleException:exception withCommand:command]; - } -} - -- (void)head:(CDVInvokedUrlCommand*)command { - manager.securityPolicy = securityPolicy; - NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; - NSDictionary *headers = [command.arguments objectAtIndex:2]; - NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:3] doubleValue]; - - [self setRequestHeaders: headers forManager: manager]; - [self setTimeout:timeoutInSeconds forManager:manager]; - [self setRedirect: manager]; - - CordovaHttpPlugin* __weak weakSelf = self; - manager.responseSerializer = [AFHTTPResponseSerializer serializer]; - [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; - - @try { - [manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - // no 'body' for HEAD request, omitting 'data' - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:nil]; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; - }]; - } - @catch (NSException *exception) { - [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; - [self handleException:exception withCommand:command]; - } -} - - (void)uploadFile:(CDVInvokedUrlCommand*)command { manager.securityPolicy = securityPolicy; NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; - NSDictionary *headers = [command.arguments objectAtIndex:2]; - NSString *filePath = [command.arguments objectAtIndex: 3]; - NSString *name = [command.arguments objectAtIndex: 4]; - NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:5] doubleValue]; + NSDictionary *headers = [command.arguments objectAtIndex:1]; + NSString *filePath = [command.arguments objectAtIndex: 2]; + NSString *name = [command.arguments objectAtIndex: 3]; + NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue]; NSURL *fileURL = [NSURL URLWithString: filePath]; @@ -425,7 +421,7 @@ [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; @try { - [manager POST:url parameters:parameters constructingBodyWithBlock:^(id formData) { + [manager POST:url parameters:nil constructingBodyWithBlock:^(id formData) { NSError *error; [formData appendPartWithFileURL:fileURL name:name error:&error]; if (error) { @@ -459,15 +455,13 @@ } } - - (void)downloadFile:(CDVInvokedUrlCommand*)command { manager.securityPolicy = securityPolicy; NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; - NSDictionary *headers = [command.arguments objectAtIndex:2]; - NSString *filePath = [command.arguments objectAtIndex: 3]; - NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue]; + NSDictionary *headers = [command.arguments objectAtIndex:1]; + NSString *filePath = [command.arguments objectAtIndex: 2]; + NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:3] doubleValue]; [self setRequestHeaders: headers forManager: manager]; [self setTimeout:timeoutInSeconds forManager:manager]; @@ -482,7 +476,7 @@ [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; @try { - [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { + [manager GET:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) { /* * * Licensed to the Apache Software Foundation (ASF) under one