Reputation: 286
I need to pass json to POST parameters. Input params look like this:
{ "type":"facebook",
"friends":[{ "id": "facebook id", "avatar": "url to avatar image", "first_name" : "first_name", "last_name" :"last_name"}] }
The server response error states it's a bad request parameter
Error - code: 400, message: {"status":"bad request","message":"undefined method `each' for #<String:0x00000005473e48>"}
It could be erroneous when I prepare the JSON data in "friends" part. Can you help me review?
Here's my code-
{
NSMutableArray *aFriends = [[NSMutableArray alloc] init];
int nCount = [[self savedSelIndexPath] count];
for (int i = 0; i < nCount && nCount > 0; i++)
{
NSIndexPath *path = [[self savedSelIndexPath] objectAtIndex:i];
NSDictionary *data = nil;
NSString *key = [[self keysArray] objectAtIndex:path.section];
NSArray *nameSection = [[self listContentDict] objectForKey:key];
data = [nameSection objectAtIndex:[path row]];
NSDictionary *dictFriends = [NSDictionary dictionaryWithObjectsAndKeys:
[data objectForKey:@"id"], @"id",
[data objectForKey:@"picture"], @"avatar",
[data objectForKey:@"first_name"], @"first_name",
[data objectForKey:@"last_name"], @"last_name",
nil];
[aFriends addObject:dictFriends];
dictFriends = nil;
}
DLog(@"aFriends: %@", aFriends);
NSDictionary *teamProfile = [[Global sharedGlobal] getPlistFile];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"facebook", @"type",
aFriends, @"friends", nil];
DLog(@"params: %@", params);
NSString *sPath = [NSString stringWithFormat:@"/users/%@/friends", [teamProfile valueForKey:@"userId"]];
NSMutableURLRequest *request = [[[Global sharedGlobal] httpClient] requestWithMethod:@"POST" path:sPath parameters:params];
DLog(@"request: %@", request);
….
[aFriends release];
}
Upvotes: 0
Views: 345
Reputation: 3006
If you want to pass JSON to the server you have to set it up like this:
NSMutableURLRequest *networkRequest;
networkRequest = [[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString:[networkServerAddress stringByAppendingString:@""]]];
send = [[json objectAtIndex:i] dataUsingEncoding:NSUTF8StringEncoding];
//Set the headers appropriately
[networkRequest setHTTPMethod:@"POST"];
[networkRequest setValue:@"application/json"
forHTTPHeaderField: @"Content-type"];
[networkRequest setValue:[NSString stringWithFormat:@"%d", [send length]]
forHTTPHeaderField:@"Content-length"];
[networkRequest setValue:@"application/json"
forHTTPHeaderField:@"Accept"];
//Set the body to the json encoded string
[networkRequest setHTTPBody:send];
Send has to be a NSData, not a dictionary. You must first parse the dictionary if you want to do it like that. There are many good parsers/writers for JSON out there, I personally like and use SBJson. With SBJson you can do something like this:
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSData *dataToSend = [writer dataWithObject:data];
Upvotes: 1
Reputation: 2417
I'm not sure if I understood what you ask, but you said you need to encode data to JSON and I can't see any JSON encode instruction for your parameters. You are simply passing a NSDictionary *dictFriends... Is it correct?
See you!
Upvotes: 0