EDIT 07/14
As Bill Burgess mentionned in a comment of his answer, this question is related to the version 1.3
of AFNetworking
. It may be outdated for the newcomers here.
I'm quite new to iPhone development, and I'm using AFNetworking as my services library.
The API i'm querying is a RESTful one, and I need to make POST requests. To do this, I tried with the following code :
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/login"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Pass Response = %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Failed Response : %@", JSON);
}];
[operation start];
There are two main issues with this code :
AFJSONRequestOperation
seems to make aGET
request, not aPOST
one.- I can't put parameters to this method.
I also tried with this code :
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient postPath:@"/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Succes : %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure : %@", error);
}];
Is there a better way to make what I want here to get it done ?
Thanks for the help !
You can override the default behavior of your request being used with
AFNetworking
to process as a POST.This assumes you have overridden the default
AFNetworking
setup to use a custom client. If you aren't, I would suggest doing it. Just create a custom class to handle your network client for you.MyAPIClient.h
MyAPIClient.m
Then you should be able to fire off your network calls on the operation queue with no problem.