Chris
Chris

Reputation: 1750

Problems with AFNetworking 2.0 POST Request

I am new to both iOS and AFNetworking. I'm used to Titanium and the very simple way of posting and getting data with a model similar to xhr in Javascript. I send a Post request to my server and get back JSON. It works flawlessly in Titanium, but I'm having a heck of a time getting it to work natively.

After doing some looking into the backend of Titanium, it appears that they are using ASI, which I found interesting. Now it seems like AFNetworking is the alternative, since ASI is no longer being updated. When I send the request below, however, it gets a text/html response and not a JSON response. I'm guessing it is because of the parameters and how they are being sent.

On a different StackOverflow question the person needed to provide standard post variables to the server and the server responded with JSON which is exactly what I need to do. The suggestion was to set the "parameterEncoding to AFFormURLParameterEncoding in your AFHTTPClient", but I don't know how to do that. If anyone can help me solve this issue I would be eternally grateful. I can provide the actual post address and dummy user/pass if that helps anyone figure this out.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"username": @"dummyusername",@"password":@"dummypassword"};
NSString *URLString = @"https://www.webaddress.com/index.php?ACT=71";
[manager POST:URLString parameters:parameters constructingBodyWithBlock:^( id<AFMultipartFormData> parameters ){} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Error: %@", error);
}];

Upvotes: 2

Views: 4751

Answers (3)

hackcat
hackcat

Reputation: 19

you can use AFNetworking like this.

AFHTTPClient *httpClient = [[AFHTTPClient alloc] init];
NSDictionary *parameter = @{@"username":@"dummyusername", @"password":@"dummypassword"};
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

[httpClient postPath:@"https://www.webaddress.com/index.php?ACT=71" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
BOOL *success = [[responseObject objectForKey:@"success"] boolValue];
NSLog(@"Response: %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self handleConnectionError:error];
}];

Upvotes: -1

HighFlyingFantasy
HighFlyingFantasy

Reputation: 3789

In AFNetworking 2.1, you can create an AFHTTPRequestOperationManager as an instance variable, initialize it in viewDidLoad of your view controller (or a setup method in an object), set the request serializers, and then make requests through the instance variable.

Working example:

// Creation
-(void)setup {
    url = [NSURL URLWithString:kMediaURLString];

    manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
}

...

-(void)makeRequestWithObject:(id)obj {
    // Create your parameters
    NSDictionary *params = @{ @"someParameter" : obj };

    // Create a path (relative to your manager's baseURL)
    NSString *urlString = [NSString stringWithFormat:kCreatePath, postPath];

    // Post the request to urlString with params, handle success or failure
    [manager POST:urlString
       parameters:params
          success:^(AFHTTPRequestOperation *operation, id success){
              // Do stuff on success
          }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              // Do stuff on failure
          }];
}

Upvotes: 4

josemando
josemando

Reputation: 573

I haven't used AFNetworking the way you are doing, but I guessing your problem is related to the server being unaware that it should send a JSON response. You could change that setting the 'accept' header to 'application/json'.

Take a look at this AFNetworking tutorial, it helped me a lot :)

Upvotes: 0

Related Questions