2012-05-25 51 views
0

我無法通過身份驗證來使用PHP堆棧上的REST API。允許登錄在瀏覽器上工作的查詢字符串看起來像​​...我遵循RestKit上的所有可用文檔,並且提供了下面的代碼,除了啓用NSLogConfigure之外,其輸出還在代碼之後。 代碼具有多個參數的RestKit身份驗證

- (void)sendHTTPAuthRequestWithParams { 
NSMutableDictionary* authParams = [[NSMutableDictionary alloc] init]; 
NSMutableDictionary* moreParams = [[NSMutableDictionary alloc] init]; 

//user and password params 
[authParams setObject:usernameTextField.text forKey:@"login"]; 
[authParams setObject:passwordTextField.text forKey:@"password"]; 

//more parameters for rest login api 
[moreParams setObject:@"Login" forKey:@"module"]; 
[moreParams setObject:@"mylogin" forKey:@"action"]; 
[moreParams setObject:authParams forKey:@"params"]; 

//parsing 
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON]; 
NSError *error = nil; 
NSString *json = [parser stringFromObject:moreParams error:&error]; 

[RKClient sharedClient].authenticationType = RKRequestAuthenticationTypeHTTP; 

//if no error 
if(!error) {   
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); 
    [[RKClient sharedClient] post:@"/index.php?" params:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON] delegate:self]; 
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); 
}  
//[[RKClient sharedClient] post:@"/index.php?module=Login&action=mylogin" params:authParams delegate:self]; } 

RKLogConfigureByName()輸出

2012-05-24 19:59:09.513 GiantsGamer[57045:207] D restkit.network:RKRequest.m:435 Sending asynchronous POST request to URL http://www.foo.com/gamer/index.php. 
2012-05-24 19:59:09.514 GiantsGamer[57045:207] T restkit.network:RKRequest.m:381 Prepared POST URLRequest '<NSMutableURLRequest http://localhost/gamer/index.php>'. HTTP Headers: { 
"Content-Length" = 85; "Content-Type" = "application/json";}. HTTP Body: {"module":"Login","action":"mylogin","params":{"login":"admin","password":"admin"}}. 2012-05-24 19:59:09.736 GiantsGamer[57045:207] D restkit.network:RKResponse.m:195 NSHTTPURLResponse Status Code: 200 2012-05-24 19:59:09.737 GiantsGamer[57045:207] D restkit.network:RKResponse.m:196 Headers: {"Cache-Control" = "no-store, must-revalidate"; 
    Connection = "Keep-Alive"; 
    "Content-Length" = 2745; 
    "Content-Type" = "text/html; charset=utf-8"; 
    Date = "Fri, 25 May 2012 02:59:09 GMT"; 
    Expires = ""; 
    "Keep-Alive" = "timeout=5, max=100"; 
    Pragma = ""; 
    Server = "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8"; 
    "Set-Cookie" = "PIWIK_SESSID=a065dba239819175689b1e2a23021d01; path=/; HttpOnly"; 
    "X-Frame-Options" = sameorigin; 
    "X-Powered-By" = "PHP/5.3.8"; 
} 
2012-05-24 19:59:09.737 GiantsGamer[57045:207] T restkit.network:RKResponse.m:203 Read response body: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> 
<head>… 
</head> 
<body>... 
</body> 
</html> 

你可以從日誌中看到,查詢字符串(認證參數)包含在HTTP主體。那很好嗎?另外,我確實得到了http響應狀態代碼200(OK),但是我只能看到index.html網頁生成的整個HTML代碼。不知道我去哪裏錯了?我如何解譯RestKit響應?

+0

您是否嘗試將所有參數作爲路徑變量上的簡單字符串發送?作爲對此的迴應,你會得到什麼? – clopez

+0

我試過這樣做,並且工作正常...但是,我必須硬編碼身份驗證值。 – rajeshvenkat

回答

1

您的代碼無法正常工作,因爲您正在將模塊和操作發送爲POST值而不是GET。您可能會收到相同的html代碼,就好像您會向index.php發送無params請求一樣。如果你願意,你可以嘗試你的代碼並捕獲整個HTML響應,並將其與index.php網頁上的HTML響應進行比較,沒有參數,我敢打賭他們是一樣的。

我沒有測試代碼,但不妨一試:

- (void)sendHTTPAuthRequestWithParams { 

    NSString *loginPath = [NSString stringWithFormat:@"index.php?module=%@&action=%@",@"Login",@"myModule"]; 

    NSMutableDictionary* authParams = [[NSMutableDictionary alloc] init]; 
    [authParams setObject:usernameTextField.text forKey:@"login"]; 
    [authParams setObject:passwordTextField.text forKey:@"password"]; 

    [[RKClient sharedClient] post:loginPath usingBlock:^(RKRequest *request) { 
     request.authenticationType = RKRequestAuthenticationTypeHTTP; 
     request.method = RKRequestMethodPOST; 
     request.params = authParams; 
     request.delegate = self; 
     request.onDidLoadResponse = ^(RKResponse *response){ 
      if (response.statusCode > 299) { 
       //We got an error! 
      }else { 
       //Check Response Body to get Data! 
      } 
     }; 

    }]; 
} 

在響應體,你會得到一個NSData,如果你希望在響應JSON可以變換到一個NSDictionary與此:NSData to NSDictionary from JSON

RK的主要缺點是,它缺乏良好的文檔,這裏有一些東西,你可以看看,瞭解一些基本知識:

可以看到另一種方式,有點更先進的,這樣做的你想用什麼RKObjectM應用RestKit:Making a post to API with RKParams and mapping the response with RKObjectMapping using RestKit

最後,你也可以檢查這個代碼,它描述瞭如何Do a simple json POST using RESTKit

你也可以問RestKit的開發者在Twitter上。這對我來說非常好,他們回答得非常快!

+0

謝謝,@clopez,這對我來說非常合適。我仍然有點困惑,爲什麼我的原始代碼不起作用。是的,我也認爲RKObjectMapping應該適用於高級查詢。 – rajeshvenkat

+0

我會更新我的回答以回答這個問題,如果你願意,請記住給我回答:) – clopez

+0

我可以問一個後續問題嗎?你如何在NSMutableDictionary中包含一個多形式的圖像? http://stackoverflow.com/questions/11189120/include-image-using-nsmutabaledictionary-with-rkclient-and-receiving-on-the-php/11190451#comment14704769_11190451 – ruelluna