codeRefiner
codeRefiner

Reputation: 513

AFNetworking returning 400

I am trying to make a simple login system

I am using AFNetworking and this is my login function as it stands:

- (void) login {

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:email.text, @"email", password.text, @"password", nil];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8888"]];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                        path:@"http://localhost:8888/api.php"
                                                  parameters:params];


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

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = [operation responseString];
    NSLog(@"response: [%@]",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@", [operation error]);
}];

[operation start];

}

It is returning status 400 "bad request" so I am assuming my post is malformed. But I can't seem to find what is wrong with it?

Here is a glance at the login APi

function login() {

    // Check for required params
    if (isset($_POST['email']) && isset($_POST['password'])) { 

        // Put params into local variables
        $email = $_POST['email'];
        $pass = $_POST['password'];

        echo 'Your email:' + $email + 'and password: ' + $password;

        // look up params in db
        $stmt = $this->db->prepare('SELECT `email`, `password`, `group_name` FROM `User` WHERE `email` = ? AND `password` = ?'); 
        $stmt->bind_param('ss', $email, $password); 
        $stmt->execute();
        $stmt->bind_result($u_email, $u_password, $group_name);
        while ($stmt->fetch()) {
            break;
        }

        $stmt->close();

        // Bail if code doesn't exist

        // Return unlock code, encoded with JSON
        $result = array(
            "group_name" => $group_name,
        );

        sendResponse(200, json_encode($result));
        return true;
    } 

    sendResponse(400, 'Invalid request');
    return false;
}  

So as you can see the email / password are not being sent with the post as their values are not set.

What is the issue here?

Upvotes: 0

Views: 329

Answers (1)

The Lazy Coder
The Lazy Coder

Reputation: 11818

get wireshark on the machine running the web host.

http://www.wireshark.org/

then you can read the network traffic coming in on the server.

perhaps its not even getting to the server. or perhaps port 8888 is blocked.

Or maybe the http request is malformed and the server does not know what to do so it returns a 400

for the filter you can use

tpc.port == 8888

this way it will only show tcp traffic on port 8888

Upvotes: 1

Related Questions