I keep receiving the following error message: 2013-01-22 01:44:43.091 Section3App2[16625:6703] -[__NSCFArray length]: unrecognized selector sent to instance 0x23a48780
after submitting my AFNetworking Request. The idea behind the request is that we are sending a post request to a REST API with a JSON Request Body via POST. I have been fiddling with this all day and can't seem to figure out whats causing the problem.
CODE
NSString *string = @"[{\"code\": \"105N14560\"}]"; NSString * jsonString = string; NSData * data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError * error = nil; id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; [request setHTTPBody:json]; // [request setValue:[NSString stringWithFormat:@"%d", string.length] forHTTPHeaderField:@"Content-Length"]; NSLog(@"request body:%@", request.HTTPBody); // NSLog(@"json: %@",json); // if (!json) { // // handle error // NSLog(@"fail"); // } AFJSONRequestOperation *operation2 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"JSON: %@", JSON); } failure:nil]; [operation2 start];
That code successfully creates the request body but when it tries to run the block it throws the error and I'm completely stumped. All help would be greatly appreciated.
json
as the argument tosetHTTPBody:
. This should be anNSData
object per the docs, but you've passed it anNSArray
by deserializing te JSON string you constructed. Just passdata
as your argument, you don't need to deserialize the JSON prior to posting it. – Carl Veazey