sabeer
sabeer

Reputation: 603

how to send array as a parameter in Afnetwoking post method?

hi i need to send a array as a one of the parameter in Afnetworking Query String

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL    URLWithString:@"http://192.008.0.28/aaa/a/"]];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"20", @"Miles", [NSArray arrayWithObjects:@"1",@"2",@"3",nil], @"Interval", nil];

    [httpClient postPath:iUpdateNotificationMethod parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Request Successful, response '%@'", responseStr);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
    }];

But server side we got "Miles":20,"Intervals":null how to fix it Thanks,

Upvotes: 1

Views: 5478

Answers (3)

Webber Lai
Webber Lai

Reputation: 2022

Try This

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:OAuthBaseURL];

NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:0];
    for (int i =0; i < [userIDs count]; i++) {
        NSString *userID = [[userIDs objectAtIndex:i] objectForKey:@"id"];
        NSDictionary *tmpDict = [NSDictionary dictionaryWithObjectsAndKeys:userID , [NSString stringWithFormat:@"ids[%i]",i], nil];
        [parameters addEntriesFromDictionary:tmpDict];
    }
    [client postPath:@"/user"
          parameters:parameters
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSData *data = (NSData *)responseObject;
                NSString *jsonStr = [[NSString alloc] initWithData:data
                                                          encoding:NSUTF8StringEncoding];
                NSLog(@"jsonStr %@",jsonStr);

            }
            failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [self showError];
            }
     ];

Upvotes: 2

KHansenSF
KHansenSF

Reputation: 604

I believe this will work: params = @{ @"Miles": @"20", @"Interval": @[@"1",@"2",@"3"] };

Upvotes: -1

StatusReport
StatusReport

Reputation: 3427

Since you're submitting an array, AFNetworking is generating a different parameter name and overloads it with the values you supply. For example, your request generates the following querystring:

Interval[]=1&Interval[]=2&Interval[]=3&Miles=20

This is defined in AFHTTPClient.m in the AFQueryStringPairsFromKeyAndValue function.

If you want to keep the original parameter, you should decide how to convert your NSArray to NSString by yourself. For example, you can do something like [myArray componentsJoinedByString:@","] and then split it back to elements on the server. If you choose this method, beware of using characters that might appear in your actual data.

Upvotes: 0

Related Questions