json PUT request
Hi iam usng JSONKit. i need to update status in linkedin site by sending the status update through json request to server. this is the code im sendin. im gettin 400 eroor. please tel me whats the mistake.
thanks.
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"linkedin-html",@"contentType",@"My Fancy Update",@"body",nil];
NSString *str =[jsonDict JSONString];
NSMutableData *requestData = [NSMutableData dataWithBytes:[str UTF8String] length:[str length]];
[self setHTTPBody:requestData];
[self setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[self setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
dont know if this will fit your usage needs exactly, I am using iphone and connecting to jayrock .net webservices.
Here is the command I use to handle all my calls.
Also, I am using the json-framework
- (NSDictionary*) sendJSONRPCRequestTo:(NSString*) url
forCommand:(NSString*)command
withParamaters:(NSMutableArray*) parameters
synchronous:(BOOL) sendSynchronous
{
if (parameters != nil)
{
[parameters setValue:[HSAppData appVersion] forKey:@"AppVersion"];
[parameters setValue:[NSNumber numberWithDouble:[HSAppData currentLocation].longitude] forKey:@"Longitude"];
[parameters setValue:[NSNumber numberWithDouble:[HSAppData currentLocation].latitude] forKey:@"Latitude"];
}
if (self.commandId == nil)
{
self.commandId = @"1";
}
NSMutableURLRequest *request = [self.baseTransaction makeNewRequestFor:url];
NSMutableDictionary *mainPackage = [NSMutableDictionary dictionary];
[mainPackage setValue:self.commandId forKey:@"id"];
[mainPackage setValue:command forKey:@"method"];
[mainPackage setValue:parameters forKey:@"params"];
NSString *jsonData = [mainPackage JSONRepresentation];
[request setValue:command forHTTPHeaderField:@"X-JSON-RPC"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
if (jsonData != nil && [jsonData isEqual:@""] == NO)
{
[request setHTTPMethod:@"POST"];
[request setValue:[[NSNumber numberWithInt:[jsonData length]] stringValue] forHTTPHeaderField:@"Content-Length"];
}
[request setHTTPBody:[jsonData dataUsingEncoding:NSUTF8StringEncoding]];
if (sendSynchronous)
{
NSHTTPURLResponse * response = nil;
NSError * error = nil;
//self.baseTransaction.lastConnection = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *jsonResult = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSDictionary *jsonDict = nil;
@try {
jsonDict = [jsonResult JSONValue];
}
@catch (NSException * e) {
NSLog(@"Error: %@",jsonResult);
jsonDict = [NSMutableDictionary dictionary];
[jsonDict setValue:self.commandId forKey:@"id"];
[jsonDict setValue:@"Unable to call function on server" forKey:@"error"];
[jsonDict setValue:[NSNull null] forKey:@"result"];
}
@finally {
return jsonDict;
}
}
// TODO: Add ASynchronous
// else
// {
// }
}
I would start by writing your json payload and the connection response data to a simple text file and review (or post, if you want us to take a look). I've found that's the easiest way for me to spot issues when posting data to services. You're using a library, so my guess is the payload should be fine, but you never know. The response data may include more hints to the true issue, although I can't say I've ever used LinkedIn's api.
Also, I didn't see where you specified that the request was a "PUT". Did you include
[req setHTTPMethod:@"PUT"];
Use this code to write the payload to the file system (sorry for the formatting, it's not playing nicely with mobile safari):
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *documentPath = [NSString stringWithFormat:@"%@/payloadData.txt", documentsDirectoryPath];
[requestData writeToFile:documentPath atomically:YES];
May be this can help you.
精彩评论