AFNetworking PUT and Delete with Rails

て烟熏妆下的殇ゞ 提交于 2019-12-08 07:33:23

问题


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?


回答1:


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];

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



来源:https://stackoverflow.com/questions/12449268/afnetworking-put-and-delete-with-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!