Search code examples
iosobjective-chttp-postafnetworking-2

AFNetworking send parameters as form data


I have been trying to send parameters as form data by setting content type as application/x-www-form-urlencoded into HTTPHeaderField but the request is not going through for only above content type, my code works for type application/json. So, I am not able to rectify the actual problem hence the request is not going through.

Below is my code ::

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setTimeoutInterval:60];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:requestAuthorization forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forHTTPHeaderField:@"device_id"];
[manager.requestSerializer setValue:[[UIDevice currentDevice] systemVersion] forHTTPHeaderField:@"device_os"];
[manager.requestSerializer setValue:[[UIDevice currentDevice] systemName] forHTTPHeaderField:@"device_name"];

[manager POST:url parameters:parameters success:success failure:failure];

Any help would be much appreciated.

Thanks


Solution

  • It is given on the AFNetworking page on Github. AFNetworking Library

    You can do this by simply using following example:-

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSDictionary *parameters = @{@"key": @"xxx"};
    
    [manager POST:@"http://test.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);}
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    }];