I'm trying to use a pattern similar to this post which describes doing a PUT/POST for RESTful API, but for a GET.
My original code looks almost the same, except I used a GET:
keyword. I quickly discovered that rather than sending a JSON body, it instead url encodes the parameters. This is not mentioned in the documentation of the AFJSONSerializer
class. You have to go to the superclass documentation (AFHTTPSerializer
) and read through its properties, where you'll find the one about HTTPMethodsEncodingParametersInURI. By default that set is populated with HEAD, GET, and DELETE. So for those types of requests, the JSON serializer apparently reverts to its parent class for behavior?
So I put together the following code:
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet set];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername: currentUser() password: currentPassword()];
[manager
GET: @"https://172.16.214.214:44321/trees"
parameters: [NSDictionary dictionary]
success:^(NSURLSessionDataTask* task, id responseObject){
NSLog(@"Response: %@", responseObject);}
failure:^(NSURLSessionDataTask* task, NSError* error){
NSLog(@"Error: %@", error);}];
The line that sets HTTPMethodsEncodingParametersInURI = [NSSet set]
is intended to let me get JSON encoded parameters like I wanted for the GET too. Unfortunately, I see nothing at the server when I use this and get the following in my error console:
2013-12-10 10:11:14.149 myValve[957:60b] Error: Error Domain=NSURLErrorDomain Code=-1005 "The
network connection was lost." UserInfo=0x17e5d4b0
{NSErrorFailingURLStringKey=https://172.16.214.214:44321/trees,
NSErrorFailingURLKey=https://172.16.214.214:44321/trees, NSLocalizedDescription=The network
connection was lost., NSUnderlyingError=0x17e53240 "The network connection was lost."}
What am I still missing?