3

Use a AFNetworking against a REST API.

  • POST to resources/
  • Server responds with 303 -> see resources/3928.json

The the server is giving information here: the id 3928 has been assigned to your resource.

Is it possible to learn this URL during the POST operation? Also, I don't really need this resource. Can I avoid actually following the redirect?

Another option is to use a 200 with {status:"ok",insert_id:3928} but it feels like this is not necessary

William Entriken
  • 37,208
  • 23
  • 149
  • 195

2 Answers2

2
-(void)call
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:YOUR_PARAMETER forKey:KEY];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:YOUR_WEBSERVICE_URL];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:WEBSERVICE_NAME parameters:params];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         //This is Redirect URL 
         NSLog(@"%@",[[[operation response] URL] absoluteString]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Failure");
     }];

     [operation start];
}
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
  • This does not work for me - it still makes the redirect before I get a look in. I tried to replace setCompletionBlockWithSuccess with setRedirectResponseBlock but this gets called with NULLs for some reason. – Tom Jul 11 '13 at 17:55
1

I know this is a bit old, but I ran into this same type of problem. What helped me achieve finding the redirect URL was the following code snippet. I was able to get my redirect url out of the jsonDict.

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id jsonObject){
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"json: %@", jsonDict);
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
    NSLog(@"failure");
}];
lramirez135
  • 2,872
  • 3
  • 18
  • 18