ios - AFNetworking URL parameter encoding for datetime with + and : sign -


i'm using afnetworking ios , want send request query parameter has datetime value. wanted behavior should be:

original: 2016-07-04t14:30:21+0200 encoded:  2016-07-04t14%3a30%3a21%2b0200 example:  .../?datetime=2016-07-04t14%3a30%3a21%2b0200 

afnetworking string encoding doesn't include special characters + / & : , few more (wikipedia: percent-encoding), fine since reserved. have encode value of datetime way escape plus , colon sign. when manually encode value before afnetworking escapes % twice obviously. puts %25 each %

2016-07-04t14%253a30%253a21%252b0200 

i want afnetworking use percent encoding query allowed characters like:

query.stringbyaddingpercentencodingwithallowedcharacters(nscharacterset.urlpathallowedcharacterset()) 

i didn't find solution change or disable encoding afnetworking manually. have suggestions?

after little more research i've found place inject encoding want. way didn't work:

encoding not working

init requestoperationmanager:

self.requestoperationmanager = [[afhttprequestoperationmanager alloc] init]; self.requestoperationmanager.requestserializer = [afjsonrequestserializer serializer]; 

use requestoperationmanager init operations

nsurlrequest *request = [nsurlrequest alloc] initwithurl:url]; // problem here afhttprequestoperation *operation = [self.requestoperationmanager httprequestoperationwithrequest:urlrequest success:^(afhttprequestoperation *operation, id responseobject) {     // success } failure:^(afhttprequestoperation *operation, nserror *error) {     // failure }]; [self.requestoperationmanager.operationqueue addoperation:operation]; [operation start]; 

way have more control

the afhttprequestserializer can create requests , can use own serialization.

init requestoperationmanager , add query string serialization block:

self.requestoperationmanager = [[afhttprequestoperationmanager alloc] init]; self.requestoperationmanager.requestserializer = [afjsonrequestserializer serializer]; [self.requestoperationmanager.requestserializer setquerystringserializationwithblock:^nsstring * _nonnull(nsurlrequest * _nonnull request, id _nonnull parameters, nserror * _nullable __autoreleasing * _nullable error) {          if ([parameters iskindofclass:[nsstring class]]) {             nsstring *yourencodedparameterstring = // every want it.             return yourencodedparameterstring;         }         return parameters;     }]; 

now change how create nsurlrequest:

nsstring *method = @"get"; nsstring *urlstringwithoutquery = @"http://example.com/"; nsstring *query = @"datetime=2016-07-06t12:15:42+0200" nsmutableurlrequest *urlrequest = [self.requestoperationmanager.requestserializer requestwithmethod:method urlstring:urlstringwithoutquery parameters:query error:nil]; 

it important split url. use url without query urlstring parameter , query parameters parameter. using requestwithmethod:urlstring:parameters:error call query string serialization block you've provided above , encode parameters want.

afhttprequestoperation *operation = [self.requestoperationmanager httprequestoperationwithrequest:urlrequest success:^(afhttprequestoperation *operation, id responseobject) {     // success } failure:^(afhttprequestoperation *operation, nserror *error) {     // failure }]; [self.requestoperationmanager.operationqueue addoperation:operation]; [operation start]; 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -