Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFHTTPSessionManager send GET request json format

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

like image 898
static0886 Avatar asked Mar 12 '26 09:03

static0886


2 Answers

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.

like image 102
fabrizotus Avatar answered Mar 13 '26 22:03

fabrizotus


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.

like image 24
runmad Avatar answered Mar 13 '26 22:03

runmad