json PUT 请求

发布于 2024-11-04 17:35:06 字数 641 浏览 0 评论 0原文

你好,我使用 JSONKit。我需要通过 json 请求向服务器发送状态更新来更新 linkedin 站点中的状态。这是我发送的代码。我的价格是 400 欧元。请告诉我有什么错误。

谢谢。

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"];

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"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

躲猫猫 2024-11-11 17:35:06

不知道这是否完全适合您的使用需求,我正在使用 iPhone 并连接到 jayrock .net Web 服务。

这是我用来处理所有呼叫的命令。

另外,我正在使用 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 
//  {
//  }

}

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 
//  {
//  }

}
甜嗑 2024-11-11 17:35:06

我首先将您的 json 有效负载和连接响应数据写入一个简单的文本文件并进行审查(或发布,如果您希望我们看一下)。我发现这是我在将数据发布到服务时发现问题的最简单方法。你正在使用一个库,所以我的猜测是有效负载应该没问题,但你永远不知道。尽管我不能说我曾经使用过 LinkedIn 的 api,但响应数据可能包含更多关于真实问题的提示。

另外,我没有看到您在哪里指定该请求是“PUT”。您是否包含

 [req setHTTPMethod:@"PUT"];

使用此代码将有效负载写入文件系统(抱歉格式,它在移动 safari 中不能很好地播放):

NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *documentPath = [NSString stringWithFormat:@"%@/payloadData.txt", documentsDirectoryPath];  
[requestData writeToFile:documentPath atomically:YES];

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];
笛声青案梦长安 2024-11-11 17:35:06

也许这个可以帮助你。

May be this can help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文