Reputation: 115
I want to use AFNetworking for a HTTP Post method with some parameters and two values have the same parameter. I tried out:
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionarydictionaryWithObjectsAndKeys: @"value1", @"param1", @"value2", @"param2", @"value3", @"param3", @"value4", @"param3", nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/index.php" parameters:params];
I used Charles Proxy and figured out that the value4 was missing in my request.
Then I tried:
NSString *parameter = @"param1=value1¶m2=value2¶m3=value3¶m3=value4";
[request setHTTPBody:[parameter dataUsingEncoding:NSUTF8StringEncoding]];
And this worked.
Now I want to know if I am doing something wrong with AFNetworking or is this a bug of AFNetworking?
Upvotes: 2
Views: 2953
Reputation: 2721
Your issue is not with AFNetworking but with NSDictionary. You cannot have two entries with the same key.
In this case it means:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"param1", @"value2", @"param2", @"value3", @"param3", @"value4", @"param3", nil];
should be replaced the following
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"param1", @"value2", @"param2", @"value3,value4", @"param3", nil];
Combining the two values for the one key.
Another way of combining arguments in one key is using a NSArray. For example:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"param1", @"value2", @"param2", [NSArray arrayWithObjects:@"value3",@"value4",nil], @"param3", nil];
Upvotes: 3
Reputation: 8905
Typo, use param4 not param3 for last key value.
NSDictionary *params = [NSDictionarydictionaryWithObjectsAndKeys: @"value1", @"param1", @"value2", @"param2", @"value3", @"param3", @"value4", @"param3", nil]
Corrected one
NSDictionary *params = [NSDictionarydictionaryWithObjectsAndKeys: @"value1", @"param1", @"value2", @"param2", @"value3", @"param3", @"value4", @"param4", nil]
Upvotes: 0