POST and PUT AFNetworking Request

I am trying to call the server. The GET call works fine and returns the correct json, but when I try to execute PUT or POST, the server returns an error.

I installed the server to receive the following 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 the server using these two methods?

+6
source share
2 answers

I would create an APIClient class for all requests instead of creating a new client every time I make a request.

See: https://github.com/AFNetworking/AFNetworking/tree/master/Example/Classes AFTwitterAPIClient.h and AFTwitterAPIClient.m

but based on your question. I believe the code will look something like this. (The code has not been 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"); }]; 

As for the mail request, just use postPath instead of putPath and it will work fine. :)

Hope I helped.

Hi,

Steve0hh

+10
source

Since no related code exists, I assume that you are using getPath:parameters:success:failure: or no parameters sent with the POST code that your server / API may require.

 postPath:parameters:success:failure: putPath:parameters:success:failure: 

Also see Request to send AFNetworking for sample code with POST with AFnetworking

+1
source

Source: https://habr.com/ru/post/917752/


All Articles