AFNetworking JSON RESTful Service parameters

504 views Asked by At

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 !

1

There are 1 answers

4
Ruchish Shah On

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/'