0

I'm new using AFNetworking and there is something I'm missing calling an API.

Here is the code (most of part the AF* example) :

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;
AFJSONRequestSerializer *reqSerializer = [AFJSONRequestSerializer serializer];
[reqSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[reqSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = reqSerializer;

AFJSONResponseSerializer *resSerializer = [AFJSONResponseSerializer serializer];
[resSerializer setReadingOptions:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers];
resSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil];
manager.responseSerializer = resSerializer;

NSString *URLString =[NSString stringWithFormat:@"%@/users/userLogin", BaseURLString];
NSDictionary *params = @{
                         @"userName": self.txtUser.text,
                         @"passWord": self.txtPass.text
                         };

[manager POST:URLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Calling the code as is the URL is: http://localhost:8000/users/userLogin

BUT with right parameters should be: http://localhost:8000/users/userLogin/JOHN/MYPASS

Finally i'm getting "Request failed: not found (404)"

The dirty workaround to test AFNetworking:

NSString *URLString = [NSString stringWithFormat:@"%@/users/userLogin/%@/%@", BaseURLString, self.txtUser.text, self.txtPass.text];

And it worked :p

Any idea what is wrong ?

Thank you for the help !

GIJOW
  • 2,307
  • 1
  • 17
  • 37
  • Parameters aren't passed how you have specified there, https://en.wikipedia.org/wiki/Query_string – Flexicoder Jun 13 '15 at 11:22
  • What does your server code look like? – Flexicoder Jun 13 '15 at 11:26
  • For instance, it's a JSON API: $app->post('/userLogin/:userName/:passWord', function($userName, $passWord) { userLogin($userName, $passWord); }); – GIJOW Jun 13 '15 at 11:28
  • If that is the case then the AFNetworking is doing the right thing. localhost won't exist on the device, are you using the simulator, is the website running on your Mac? – Flexicoder Jun 13 '15 at 11:32
  • Exactly, I'm using the simulator. The issue is the url format, the server is getting the baseurl without parameters – GIJOW Jun 13 '15 at 11:33
  • Besides the localhost. The server attempt for this: "users/userLogin/JOHN/MYPASS" and is getting this "users/userLogin" – GIJOW Jun 13 '15 at 11:34
  • Your code looks fine. Just a shot in the dark, try this - NSDictionary *params =[NSDictionary dictionaryWithObjectsAndKeys: userName, @"username", password, @"password", nil]; //See if this works – Sam B Jun 13 '15 at 11:47
  • http://stackoverflow.com/questions/7623275/afnetworking-post-request – Sam B Jun 13 '15 at 11:47
  • Thank you Sam B, but still having same issue. Is like the object is ignoring the parameters, the server can't even see it in any format. Straaannge..... – GIJOW Jun 13 '15 at 12:02

1 Answers1

0

Make sure your path to resource should be right. 404 error will be generated when URL path is not valid or server couldn't find requested resource. and if you are passing post paramater then server should handle post parameter like that only. URL should be like this 'http://localhost:8000/users/userLogin/'

Ruchish Shah
  • 319
  • 1
  • 6
  • Thank you but is not what I'm trying to reach. Let's ignore the http..... this is my route "users/userLogin/" and after that, I have parameters which will be like "users/userLogin/ME/MYPASS" and the server can process the request. In any lack of parameter, the server will answer 404. – GIJOW Jun 13 '15 at 12:03
  • then you don't have to pass parameter in post request, you need to create request like that only i.e. users/userLogin/ME/MYPASS – Ruchish Shah Jun 13 '15 at 12:11
  • Like this (as I mentioned in the post, dirty workaround) ? NSString *URLString = [NSString stringWithFormat:@"%@/users/userLogin/%@/%@", BaseURLString, self.txtUser.text, self.txtPass.text]; – GIJOW Jun 13 '15 at 12:13
  • uow... ok. Thank you for your help, will try to figure out how to adapt the server to use in the 'normal way' – GIJOW Jun 13 '15 at 12:18