1

i have a weird problem. so i've been working on an iphone app where a user can login, and upon logging in, the app would authorize the user by making an http GET request to the server. if the username and password is correct it return an authorization token (typical stuff).

the weird part is that when i test this locally, pointing the http request url to my local machine, it works fine. everything authorizes correctly and all data is returned correctly.

but today, i got the app stable enough that i decided to deploy rest api to my server, but when changed the http request url to my server url, the requests fails.

i thought maybe the server wasn't deployed correctly so i tested the http endpoints with the firefox rest client and everything seems to be working, authorization works, and data gets returned.

does anyone have any thoughts? i'm lost as to what the problem might be.

i'm using AFNetworking library to make the requests.

here's my code:

self.apiHttpClient = [[TKRHttpClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://mywebsite.com/api"]];

--------------

NSString *username = [usernameField text];
NSString *password = [passwordField text];
TKRHttpClient *httpClient = [TKRHttpClient sharedInstance];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"/user/auth" parameters:nil];
[request addValue:username forHTTPHeaderField:@"username"];
[request addValue:password forHTTPHeaderField:@"password"];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    if(response.statusCode == 200){
        NSDictionary * headers = [response allHeaderFields];
        NSString *token = [headers objectForKey:@"auth_token"];
        NSLog(@"token was %@", token);
        [[AppController sharedAppController] initializeDataForUserToken:token];
    }else{
        NSLog(@"upload failed!");
    }
} failure:^(NSURLRequest *request , NSHTTPURLResponse *response , NSError *error , id JSON ) {
    NSLog(@"code was: %i", response.statusCode);
    if(response.statusCode == 401){
        [errorMessage setText:@"Incorrect password or username"];
    }
}];
[operation start];

i put in break points, and it seems that the request goes straight to the failure block. the line that says "NSLog(@"code was: %i", response.statusCode);" in the failure block prints out 200. but when i tail the logs on my server, no request was received.

any help would be greatly appreciated. can't figure out for the life of my why it would work against my local tomcat, but won't work when deployed to my server.

thanks in advance!

4

1 回答 1

0

感谢@Brad 的建议,我找到了问题的原因。

在检查 charles 中的请求后,我发现请求是“http://mywebsite.com”,然后路径是 /user/auth。所以“/ api”部分丢失了。

也许这是标准,我不知道,但似乎 AFHTTPClient 的(我的 TKRHttpClient 扩展了 AFHTTPClient)initWithBaseURL 确实意味着主机 URL。“/api”部分被砍掉了。

它在本地工作的原因是因为我在本地以 root 身份部署了应用程序,所以没有“/api”

所以有两种方法可以解决这个问题。1.以root身份重新部署为应用程序到服务器。或 2. 更改代码行:

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"/user/auth" parameters:nil];

至:

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"api/user/auth" parameters:nil];

希望这对将来使用 AFNetworking 的人有所帮助。

于 2012-04-30T13:56:16.130 回答