1
votes

It's been two days that I'm looking for a simple code for RestKit API for logging into a website without using JSON. Here is the code that I wrote so far:

- (void)login
{
    [RKClient clientWithBaseURLString:@"http://Mywebsite.com/login.php"];
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:@"user" forKey:@"username"];
    [params setObject:@"pass" forKey:@"password"];
    [params setObject:@"login" forKey:@"type"];
    [[RKClient sharedClient] post:@"/login" params:params delegate:self];

}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
    NSLog(@"HTTP status code:     %d", response.statusCode);
    NSLog(@"HTTP status message:  %@", [response localizedStatusCodeString]);
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    NSRange range = [[error localizedDescription] rangeOfString:@"-1012"];
    if (range.length > 0){
        //Do whatever here to handle authentication failures
    }
    RKLogError(@"Hit error: %@", error);
}

Here is the log that I receive:

2012-09-08 21:44:14.633 RestKitTest[889:fb03] I restkit:RKLog.m:33 RestKit initialized...
2012-09-08 21:44:15.010 RestKitTest[889:fb03] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x6c5a560 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=NO reachabilityFlags=-R -----l->
2012-09-08 21:44:21.021 RestKitTest[889:fb03] I restkit.network:RKRequest.m:676 Status Code: 404
2012-09-08 21:44:21.036 RestKitTest[889:fb03] HTTP status code:     404
2012-09-08 21:44:21.090 RestKitTest[889:fb03] HTTP status message:  not found

Any idea how I can fix this issue would be appreciated.

2
@hlfcoding Do you have any idea how to fix this issue? - Hamid
Why don't you want to use JSON? - Abhi Beckert
I'd say it 404s because you set baseURL as http://Mywebsite.com/login.php yet you POST as post:@"/login". Do you really want to access http://Mywebsite.com/login.php/login? - mja
@mja Thank you so much, this is exactly the problem, if you post your answer I will accept it as an answer. - Hamid
@AbhiBeckert Because the website that I want to use its web service, doesn't give me the JSON as a response. - Hamid

2 Answers

2
votes

I'd say it 404s because you set baseURL as http://Mywebsite.com/login.php yet you POST as post:@"/login". Do you really want to access http://Mywebsite.com/login.php/login?

The baseURL should be set to a common prefix to all your backend calls, in your case http://Mywebsite.com, the resource itself should be posted as post:@"/login.php". The resource name and baseURL are concatenated before the request is sent.

0
votes

You'll need to provide info on what login.php is doing. I don't see why it should 404 if the script url resolves correctly.

Also, if that PHP script does a redirect on success, you should use the RKClient method post:usingBlock: and inside that block do request.followRedirect = NO;. That way you'll get the correct status code, if you are returning one.

If may also help to log more of the response:

NSLog(@"Header fields: %@", response.allHeaderFields);
NSLog(@"Body: %@", response.bodyAsString);