1

I send a registration message to the server like this:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                   timeoutInterval:10.0];

[request setHTTPMethod:@"POST"];
[request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[request setValue:API_KEY forHTTPHeaderField:@"api_key"];

NSString *dataString = [NSString stringWithFormat:@"username=%@&password=%@", username, encryptedPassword];
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];

// Connection
NSURLResponse *response = nil;
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

// Print results
NSLog(@"response: %@", response);
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
    NSLog(@"response: %d", [(NSHTTPURLResponse *)response statusCode]);
}
id jsonObject = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:&jsonError];
NSLog(@"jsonObject: %@", jsonObject);

The connection was success, but I got the error response:

=> response: 400
=> jsonObject: {
    error = "Please provide username.USERNAME:";
}

The server received a null user name, but I'm sure I pass a valid user name to http body (converted into NSData). The data contains the following info (converted back into string):

username=cc24@cc.com&password=12345678nW7wMNio0bsNBHGqY7VgSw%3D%3D

I don't what happened. But the API works fine with Android app.

Xaree Lee
  • 3,188
  • 3
  • 34
  • 55

0 Answers0