1

Been using AFNetworking 2.0 for the past two projects for a medium sized project to great success.

This one call though, to post a user' login info, keep failing on me (server states incorrect login & password, login is the email, we've check on the server and the values are the same I'm trying to send)

The server developer does not handle iOS / ObjC (java / mongoDB guy), All I'm giving is a URL and the parameters I need to pass with Content-Type = application/x-www-form-url-encoded

I read somewhere that characters such as @ (in the email) could cause problems

Could anyone with more experience than me here chip in? my operation creation method is below, any help is TRULY appreciated.

-(AFHTTPRequestOperation *)loginUserWithParameters:(NSDictionary *)loginParameters
{
    /*
         parameters dictionary:
         email
         psw
         deviceToken
     */

    NSString *url=SERVER_USER_LOGIN;

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:url]];
    //    [httpClient setParameterEncoding:AFFormURLParameterEncoding];

    NSMutableURLRequest *request=[httpClient requestWithMethod:@"POST" path:nil parameters:nil];


    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:loginParameters options:kNilOptions error:nil];


    [request setHTTPBody:jsonData];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [AFNetworkActivityIndicatorManager sharedManager].enabled=YES;

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    return operation;
}
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
David Homes
  • 2,725
  • 8
  • 33
  • 53

1 Answers1

2

Actually you say with your code that you will send url-encoded data to your server, but you are overriding the postBody of your request with custom data. So you should create your request with:

NSMutableURLRequest *request=[httpClient requestWithMethod:@"POST" path:nil parameters:loginParameters];

And you should delete below lines:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:loginParameters options:kNilOptions error:nil];
[request setHTTPBody:jsonData];

I think above changes will be enough.

ismailgulek
  • 1,029
  • 1
  • 11
  • 8
  • thank you ismail! +1000 for you! had been struggling with this for the past day, the bode of the function was actually copied from a very similar one so i guess that's what I get from copying / pasting code – David Homes Apr 05 '14 at 22:59
  • 1
    @dhomes You're welcome, that's what almost everybody gets from copy/paste. – ismailgulek Apr 06 '14 at 06:10