AFNetworking POST to REST webservice

后端 未结 2 1239
清酒与你
清酒与你 2020-12-14 12:55

Brief backstory, our previous developer used ASIHTTPRequest to make POST requests and retrieve data from our webservice. For reasons unknown this portion of our app stopped

相关标签:
2条回答
  • 2020-12-14 13:57

    These are the essential (stripping out conditions I've made for my own use) lines that ended up satisfying my request to the web service. Thanks for the suggestions @8vius and @mattt !

    - (IBAction)loginButtonPressed {        
        NSURL *baseURL = [NSURL URLWithString:@"https://www.example.com/api/class"];
    
        //build normal NSMutableURLRequest objects
        //make sure to setHTTPMethod to "POST". 
        //from https://github.com/AFNetworking/AFNetworking
        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
        [httpClient defaultValueForHeader:@"Accept"];
    
        NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                [usernameTextField text], kUsernameField, 
                                [passwordTextField text], kPasswordField, 
                                nil];
    
        NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
              path:@"https://www.example.com/api/class/function" parameters:params];
    
        //Add your request object to an AFHTTPRequestOperation
        AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] 
                                          initWithRequest:request] autorelease];
    
        //"Why don't I get JSON / XML / Property List in my HTTP client callbacks?"
        //see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
        //mattt's suggestion http://stackoverflow.com/a/9931815/1004227 -
        //-still didn't prevent me from receiving plist data
        //[httpClient registerHTTPOperationClass:
        //         [AFPropertyListParameterEncoding class]];
    
        [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    
        [operation setCompletionBlockWithSuccess:
          ^(AFHTTPRequestOperation *operation, 
          id responseObject) {
            NSString *response = [operation responseString];
            NSLog(@"response: [%@]",response);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error: %@", [operation error]);
        }];
    
        //call start on your request operation
        [operation start];
        [httpClient release];
    }
    
    0 讨论(0)
  • 2020-12-14 13:57

    Use AFHTTPClient -postPath:parameters:success:failure:, passing your parameters (nested dictionaries/arrays are fine). If you're expecting a plist back, be sure to have the client register AFPropertyListRequestOperation.

    In any case, setValue:forHTTPHeaderField: is not what you want here. HTTP headers are for specifying information about the request itself; data is part of the request body. AFHTTPClient automatically converts parameters into either a query string for GET requests or an HTTP body for POST, et al.

    0 讨论(0)
提交回复
热议问题