2012-08-09 57 views
0

我想用RestKit做一個相當基本的HTTP PUT。我不想放置整個對象,因爲API調用被設計爲接受單個查詢參數並只更新該字段。到目前爲止,我嘗試了兩種方法,都是不成功的。RestKit PUT不工作

網址張貼到:https://myserver/api/users/{userId}

查詢字符串參數:verificationCode=

用法示例:PUT https://myserver/api/users/101?verificationCode=646133

方法一:將查詢參數在RKParams對象,並與那些PUT通話PARAMS。

NSString *putUrl = [NSString stringWithFormat:@"/api/users/%i", [APIUserInfo sharedAPIUserInfo].apiUserIdx]; 
    NSLog(@"the PUT url is %@", putUrl); 

    // Send a PUT to a remote resource. The dictionary will be transparently 
    // converted into a URL encoded representation and sent along as the request body 
    NSDictionary* paramsDict = [NSDictionary dictionaryWithObject:[_verificationCode text] forKey:@"verificationCode"]; 
    // Convert the NS Dictionary into Params 
    RKParams *params = [RKParams paramsWithDictionary:paramsDict]; 

    [[RKClient sharedClient] put:putUrl params:params delegate:self]; 

方法#2:構建整個URL並嘗試一個PUT,並將params設置爲nil。

NSString *putUrl = [NSString stringWithFormat:@"/api/users/%i?verificationCode=%@", [APIUserInfo sharedAPIUserInfo].apiUserIdx, [_verificationCode text]]; 
    NSLog(@"the PUT url is %@", putUrl); 

    [[RKClient sharedClient] put:putUrl params:nil delegate:self]; 

這兩種方法都不適用於我。第一個失敗說「RestKit被要求重新發送一個新的主體流以處理請求。可能的連接錯誤或身份驗證挑戰?」然後運行約10秒鐘並超時。第二種方法失敗說HTTP狀態405 - 方法不允許。

任何人都可以指出我要出錯的地方,還是提供一個簡單的使用RestKit的PUT示例?我在這裏找到的大多數例子都是把我不想做的整個對象放在這種情況下。


UPDATE:

方法2行之有效一旦我在服務器端整理出幾件事情。最終解決方案:

NSString *putUrl = [NSString stringWithFormat:@"/api/users/verify/%i?verificationCode=%@", [APIUserInfo sharedAPIUserInfo].apiUserIdx, [_verificationCode text]]; 
NSLog(@"the PUT url is %@", putUrl); 

[[RKClient sharedClient] put:putUrl params:nil delegate:self]; 

回答

1

您的網絡服務器上禁用HTTP PUT方法。出於安全原因,它默認在所有網絡服務器上。

HTTP Status 405 - Method Not Allowed. 
+0

我會告訴後端人。你能評論我應該使用哪種方法嗎? – 2012-08-09 14:47:45

+0

我不是一個休息的專家,但如果你打算使用休息,我想你只需要在服務器上啓用PUT,你應該很好。有安全隱患,但你的後端人員將不得不與你一起工作。 – 2012-08-10 04:55:10

+0

感謝您的幫助。我和他們交談,他們發現他們正在解決的安全配置問題。至於__how__我認爲方法2是正確的。當我得到最終的實施工作時,我會進行更新。 – 2012-08-10 15:20:49