POST和PUT请求AFNetworking [英] POST and PUT request AFNetworking
问题描述
我正在尝试拨打服务器。 GET调用工作正常并返回正确的json,但是当我尝试执行PUT或POST时,服务器返回错误。
I'm trying to do a call to a server. The GET call is working great and returns the correct json, but when I try to do a PUT or a POST, the server returns an error.
我将服务器设置为接收下一条消息:
I set the server to receive the next messages:
method POST
curl -X POST -d "number=NUMBER&name=NAME&lat=32.5713&lon=60.3926" http://server.com/users/
method PUT
curl -X PUT -d "number=USER&name=NAME6&lat=-34.5552&lon=32.3333" http://server.com/users/
我该怎么办?用这两种方法调用服务器?
How can I call to the server with these two methods?
推荐答案
我会为所有请求创建一个APIClient类,而不是每个都创建一个新的客户端我提出要求的时间。
I would create a APIClient class for all requests instead of creating a new client every time i make a request.
请参阅: https: //github.com/AFNetworking/AFNetworking/tree/master/Example/Classes
AFTwitterAPIClient.h& AFTwitterAPIClient.m
See : https://github.com/AFNetworking/AFNetworking/tree/master/Example/Classes AFTwitterAPIClient.h & AFTwitterAPIClient.m
但基于您的问题。
我相信代码看起来像这样。 (代码未经过测试)
but based on your question. I believe the code will look something like this. (Code was not tested)
NSURL *url = [NSURL URLWithString:@"http://server.com"];
AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];
//depending on what kind of response you expect.. change it if you expect XML
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:
@"NUMBER",@"number",
@"NAME",@"name",
@"32.5713",@"lat",
@"60.3926",@"lon",
nil];
[client putPath:@"users" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure");
}];
至于发布请求..只需使用postPath而不是putPath,它就可以正常工作。 :)
As for the post request.. just use postPath instead of putPath and it'll work fine. :)
希望我帮助过。
问候,
Steve0hh
这篇关于POST和PUT请求AFNetworking的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!