William Entriken
William Entriken

Reputation: 39263

AFNetworking -- get redirect URL

Use a AFNetworking against a REST API.

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

Upvotes: 3

Views: 4048

Answers (2)

lramirez135
lramirez135

Reputation: 3072

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");
}];

Upvotes: 1

Mehul Mistri
Mehul Mistri

Reputation: 15147

-(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];
}

Upvotes: 2

Related Questions