AFNetworking PUT和使用Rails删除 [英] AFNetworking PUT and Delete with Rails

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

问题描述

在看AFNetworking文档中,PUT和DELETE方法采取的路径和参数的字典。我使用Rails作为我的后端其中预计这两类采取认沽/object/1.json的形式和Delete /object/1.json。我应该在标识中加入建立一个路径字符串还是我送认沽或删除与ID在字典中则params的吗?

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 看那儿是如何格式化你的基地址和您的路径的示例。有一个关于在<一个这些方法的详细信息href=\"http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/getPath:parameters:success:failure:\"相对=nofollow> 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 PUT和使用Rails删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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