6

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?

Raptor
  • 53,206
  • 45
  • 230
  • 366
bubudrc
  • 159
  • 4
  • 11
  • 1
    What does this have to do with AFNetworking, exactly? Are you using AFN to make these requests? CAn you post code examples of how you're using AFN? – mattt Jun 11 '12 at 14:47

2 Answers2

10

I would create a 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 & 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");
}];

As for the post request.. just use postPath instead of putPath and it'll work fine. :)

Hope I've helped.

steve0hh
  • 637
  • 6
  • 15
1

Since there is no associated code, I assume you use getPath:parameters:success:failure: or no parameters sent with POST reqd which might be required by your server/API.

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

Also refer AFNetworking Post Request for sample code with POST with AFnetworking

Community
  • 1
  • 1
palaniraja
  • 10,432
  • 5
  • 43
  • 76