AFNetworking PUT and Delete with Rails



在查看AFNetworking文档时,Put 和 Delete 方法采用路径和参数字典。 我正在使用 Rails 作为我的后端,它希望这两种类型采用 Put/object/1.json 和 Delete/object/1.json 的形式。 我应该通过添加 Id 来构建路径字符串,还是将 Id 作为字典中的参数之一发送放置或删除?

通常当我使用PUT和类似类型的HTTP请求时,使用AFNetworking是这样的:

// Create an HTTP client with your site's base url
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://example.com"]];
// Setup the other parameters you need to pass
NSDictionary *parameters = @{@"username" : @"bob", @"password" : @"123456"};
// Create a NSURLRequest using the HTTP client and the path with your parameters
NSURLRequest *request = [client requestWithMethod:@"PUT" path:@"object/1.json" parameters:parameters];
// Create an operation to receive any response from the server
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    // Do stuff
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    // Handle error
}];
// Begin the operation
[operation start];

如果您查看AFHTTPClient.h有一些示例说明如何设置基本 URL 和路径的格式。AFNetworking文档中有关于这些方法的更多信息

最新更新