4

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?

Community
  • 1
  • 1
Travis Griggs
  • 18,930
  • 17
  • 76
  • 137
  • I wish that was actually an answer @Rob. The link helped make it clear why the framework was probably not supporting it. Thanks. – Travis Griggs Dec 20 '13 at 21:43
  • I have posted it as an answer. I just didn't want to seem like I was taking credit for other people's excellent answers. Likewise, I'm always hesitant to post an answer that can be read as "don't do it." lol. – Rob Dec 20 '13 at 22:25

2 Answers2

2

The rationale behind AFNetworking's behavior here is probably most concisely explained in the Stack Overflow question, HTTP GET with request body. One of those answers quotes Ray Fielding:

... In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

So, yes, you can send a body with GET, and no, it is never useful to do so.

The HTTP/1.1 spec defines GET to "retrieve whatever information (in the form of an entity) is identified by the Request-URI" and does not contemplate information to be included in the body of the request. So the use of the body in GET request technically might not be prohibited, but, at best, it is non-standard. AFNetworking's choice not to support it is not entirely surprising.

So, you might not want to put JSON in the body for a GET requests. One would generally add the parameters to the URL. If you want to send JSON in the body of the request, then do a POST.

Community
  • 1
  • 1
Rob
  • 371,891
  • 67
  • 713
  • 902
0

Sounds like it can't connect to the server, have you tried to make a call outside iOS?

For reference I normally make a JSON request like this:

NSURL *url = @"https://172.16.214.214:44321/trees";
NSData* data = [NSData dataWithContentsOfURL: url];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
christopher_h
  • 717
  • 7
  • 14