Created
March 31, 2013 21:58
-
-
Save mteece/5282162 to your computer and use it in GitHub Desktop.
AFNetworking and NSMutableURLRequest generate a request with params.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Generate a request with params. | |
POST /post/url | |
Host: www.example.com | |
Content-Type: application/x-www-form-urlencoded; charset=utf-8 | |
uuid=12345-ABC-9876-XYZ&first_name=john&last_name=doe | |
*/ | |
NSString *urlString = @"http://www.example.com/post/url"; | |
NSURL *baseURL = [NSURL URLWithString:urlString]; | |
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; | |
[httpClient defaultValueForHeader:@"Accept"]; | |
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: | |
@"12345-ABC-9876-XYZ", @"uuid", | |
@"john", @"first_name", | |
@"doe", @"last_name", | |
nil]; | |
NSMutableURLRequest *request = [httpClient requestWithMethod: @"POST" path: urlString parameters: params]; | |
NSLog(@"%@", [request description]); | |
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request]; | |
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; | |
[operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSString *response = [operation responseString]; | |
NSLog(@"response: [%@]", response); | |
// Manage response from server here. | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"error: %@", [operation error]); | |
}]; | |
[operation start]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment