AFHTTPClient*httpClient=[[AFHTTPClientalloc]initWithBaseURL:[NSURLURLWithString:@"http://samwize.com/"]];NSMutableURLRequest*request=[httpClientrequestWithMethod:@"GET"path:@"http://samwize.com/api/pigs/"parameters:nil];AFHTTPRequestOperation*operation=[[AFHTTPRequestOperationalloc]initWithRequest:request];[httpClientregisterHTTPOperationClass:[AFHTTPRequestOperationclass]];[operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation,idresponseObject){// Print the response body in textNSLog(@"Response: %@",[[NSStringalloc]initWithData:responseObjectencoding:NSUTF8StringEncoding]);}failure:^(AFHTTPRequestOperation*operation,NSError*error){NSLog(@"Error: %@",error);}];[operationstart];
POST
POST a urlencoded form name=piggy in the http body.
1234567
AFHTTPClient*httpClient=[[AFHTTPClientalloc]initWithBaseURL:[NSURLURLWithString:@"http://samwize.com/"]];[httpClientsetParameterEncoding:AFFormURLParameterEncoding];NSMutableURLRequest*request=[httpClientrequestWithMethod:@"POST"path:@"http://samwize.com/api/pig/"parameters:@{@"name":@"piggy"}];// Similar to GET code ...
If you want to POST a json such as {"name":"piggy"}, you change the encoding: