AFNetworking 使用 Rails 进行 PUT 和 Delete [英] AFNetworking PUT and Delete with Rails

查看:24
本文介绍了AFNetworking 使用 Rails 进行 PUT 和 Delete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In looking at the AFNetworking documentation, the Put and Delete methods take in a path and a dictionary of parameters. I am using Rails as my backend which expects these two types to take the form of Put /object/1.json and Delete /object/1.json. Should I build up a path string by adding in the Id or do I send a Put or Delete with the Id as one of the params in the Dictionary?

推荐答案

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

Typically when I do with PUT and similar type HTTP requests when using AFNetworking is something like this:

// 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 文档

If you look in AFHTTPClient.h there are examples for how to format your base url and your paths. There's more information on these methods in the AFNetworking documentation

这篇关于AFNetworking 使用 Rails 进行 PUT 和 Delete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆