AFNetworking PUT和使用Rails删除

时间:2012-09-16 17:57:16

标签: ruby-on-rails-3 api http afnetworking

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

1 个答案:

答案 0 :(得分:0)

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

// 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,可以找到有关如何格式化基本网址和路径的示例。 AFNetworking documentation

中有关于这些方法的更多信息