PUT 與 POST 都可用來傳送資料,但 PUT 通常代表把資源存放到指定位置,且在 HTTP 語意中具有幂等特性。
AFNetworking 的 PUT 方法主要提供 URLString 與 parameters,若要上傳大量資料或自訂 body,可能不如 POST 方便。
若專案仍使用 ASIHTTPRequest,可把資料 appendPostData,再將 requestMethod 設為 PUT,即可上傳。
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:uploadUrl]]; [request appendPostData:data]; [request setRequestMethod:@"PUT"]; [request startAsynchronous];
在實際專案中,也可改用 NSURLSession 自行建立 PUT 的 NSMutableURLRequest,並設定 HTTPBody 與 HTTPMethod。