2012-06-12 244 views
1

我有以下API調用:AFNetworking HTTP PUT請求

URL: /api/some-call 
Method: PUT 
PARAMS: No params 

它只是一個簡單的PUT方法。我正嘗試使用AFNetworking來做到這一點,不幸的是,我失敗了。這是我現在所擁有的:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
NSMutableURLRequest *req = [httpClient requestWithMethod:@"PUT" path:@"" parameters:nil]; 

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

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Success"); 
} failure: ^(AFHTTPRequestOperation *operatn, NSError *error) { 
    NSLog(@"Failure"); 
}]; 

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
[queue addOperation:operation]; 

但是,這是行不通的。這是爲什麼?此外,什麼是path應該在PUT請求中?我已經嘗試了幾件事情,這是我現在最後的結果,我相信應該接近正確。

最後一個問題:AFNetworking不使用ARC。這是否意味着在NSOperationQueue聲明的末尾仍然需要autorelease

編輯: 這是錯誤NSLogFailure Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code in (200-299), got 409" UserInfo=0x7a91fb0 {NSErrorFailingURLKey=*the url*/api/some-call, NSLocalizedDescription=Expected status code in (200-299), got 409}

+0

你可以註銷錯誤,所以我們可以幫你嗎? 在失敗塊中使用NSLog(@「Error:%@」,error);並向我們顯示記錄的錯誤。 – steve0hh

+0

已添加。你走了。謝謝! – darksky

+0

已回答。請檢查下面的答案。 :) – steve0hh

回答

1

嘛。您正在收到409錯誤代碼。

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html報價:

10.4.10 409 Conflict

The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough

information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

這意味着該錯誤是由您的服務器代碼引起了不。除非你提供了一些錯誤的參數。

好吧。至於關於「PUT應該是什麼路徑」的問題。 通常我會把baseURL作爲服務器的域名。 這有點像

http://localhost 

然後我就把路徑是這樣的

@"the/rest/of/the/api/url" 

那麼它更容易,只需一的基本URL開關研發和生產服務器之間進行切換。 :)

對於你的最後一個問題,「AFNetworking不使用ARC,這是否意味着我仍然需要在NSOperationQueue聲明結尾的autorelease?」

這是否意味着您的項目正在使用ARC與AFnetworking,或者AFNetworking WITH ARC。

如果是帶AFNetworking的ARC,則不需要。看看這個 https://github.com/AFNetworking/AFNetworking#arc-support

如果它是非ARC與AFNetworking,你基本上必須自己做所有的內存管理。 :) 再次打我,如果你需要更多的信息,我會相應地編輯。 :)

希望我在某種程度上幫了忙。

+0

它通過做你說的以及添加HTTP標頭。 HTTP標頭始終是一項要求嗎? – darksky

+0

這取決於你在談論什麼類型的HTTP頭。 AFNetworking將查看標題中的錯誤代碼。 200-299會擊中成功塊,而其他任何東西都會成功。將擊中失敗區塊。 它也取決於你的AFHttpClient從哪個類繼承,AFJSONRequestOperation類(需要返回JSON的內容類型)還是AFXMLRequestOperation類(需要返回XML類型的內容類型) – steve0hh