3

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

static0886
  • 784
  • 9
  • 25
  • 2
    Have you tried to percent escape the json you're appending? – Brandon May 03 '14 at 18:10
  • @user2766755: that's exactly what solved it!! thx a lot. Please add as answer to accept! I applied stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding to paramsstring, and voila! Lesson of the day: when using "(id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL", make sure all characters are properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding. – static0886 May 03 '14 at 19:35

2 Answers2

8

Yes you can:

NSString *stringCleanPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

I had to use it for parse.com's rest api and AFNetworking, and work it perfect.

fabrizotus
  • 2,958
  • 2
  • 21
  • 27
  • i use like this : [@"?where={\"id\":\"3\"}" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] in my log i see this ?where=%7B%22id%22:%223%22%7D and worked well – EFE May 09 '16 at 14:03
2

You cannot send a dictionary as a GET request. You should change your API to inspect parameters as a POST dictionary. What you're essentially trying to do is send POST request parameters above.

Or... What you may want to do is change it to a string:

NSString *params = [NSString stringWithFormat:@"\"api_key\":\"%@\",\"query\":{\"perpage\":%@}}", kAPIKey, @50];

But honestly, that's kind of messy.

runmad
  • 14,846
  • 9
  • 99
  • 140