I'm trying to send a request to web service using AFHTTPSessionManager
Here's my problem: I want to send the URL GET request with the parameters in JSON format. Like this: http://api.mysite.com/v2/json/search?parameters={“api_key”:”YOUR_API_ KEY”,”query”:{“perpage”:50}}
I have subclassed AFHTTPSessionManager. Here's what my code looks like:
+ (MyAPIClient *)sharedClient {
static MyAPIClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kBaseURLString]];
});
return _sharedClient;
}
and the initialiser:
- (instancetype)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.requestSerializer = [AFJSONRequestSerializer serializer];
return self;
}
So here's how I did it:
- (void)sendMyQueryWithSuccess:
(void(^)(NSURLSessionDataTask *task, id responseObject))success
andFailure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure{
NSDictionary *params = @{@"api_key" : kAPIKey,
@"query": @{@"perpage": @50}};
NSString *paramsstring = [[NSString alloc]
initWithData:[NSJSONSerialization
dataWithJSONObject:params
options:0
error:nil]
encoding:NSUTF8StringEncoding];
NSString* path = [NSString stringWithFormat:@"search_sale?parameters=%@",paramsstring];
[self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
if (success) {
success(task, responseObject);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(task, error);
}
}];
}
I keep getting this:
* Assertion failure in -[AFJSONRequestSerializer requestWithMethod:URLString:parameters:error:], AFNetworking/AFNetworking/AFURLRequestSerialization.m:277
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: URLString'
So what's the problem here?
when I print the value of "path" I get exactly what I want:
search?parameters={"api_key":"SOME_API_KEY","query":{"perpage":10}}
Thanks a lot for any input