我想用RestKit做一个相当基本的HTTP PUT。我不想放置整个对象,因为API调用被设计为接受单个查询参数并只更新该字段。到目前为止,我尝试了两种方法,都是不成功的。RestKit PUT不工作
网址张贴到:https://myserver/api/users/{userId}
查询字符串参数:verificationCode=
用法示例:PUT https://myserver/api/users/101?verificationCode=646133
方法一:将查询参数在RKParams对象,并与那些PUT通话PARAMS。
NSString *putUrl = [NSString stringWithFormat:@"/api/users/%i", [APIUserInfo sharedAPIUserInfo].apiUserIdx];
NSLog(@"the PUT url is %@", putUrl);
// Send a PUT to a remote resource. The dictionary will be transparently
// converted into a URL encoded representation and sent along as the request body
NSDictionary* paramsDict = [NSDictionary dictionaryWithObject:[_verificationCode text] forKey:@"verificationCode"];
// Convert the NS Dictionary into Params
RKParams *params = [RKParams paramsWithDictionary:paramsDict];
[[RKClient sharedClient] put:putUrl params:params delegate:self];
方法#2:构建整个URL并尝试一个PUT,并将params设置为nil。
NSString *putUrl = [NSString stringWithFormat:@"/api/users/%i?verificationCode=%@", [APIUserInfo sharedAPIUserInfo].apiUserIdx, [_verificationCode text]];
NSLog(@"the PUT url is %@", putUrl);
[[RKClient sharedClient] put:putUrl params:nil delegate:self];
这两种方法都不适用于我。第一个失败说“RestKit被要求重新发送一个新的主体流以处理请求。可能的连接错误或身份验证挑战?”然后运行约10秒钟并超时。第二种方法失败说HTTP状态405 - 方法不允许。
任何人都可以指出我要出错的地方,还是提供一个简单的使用RestKit的PUT示例?我在这里找到的大多数例子都是把我不想做的整个对象放在这种情况下。
UPDATE:
方法2行之有效一旦我在服务器端整理出几件事情。最终解决方案:
NSString *putUrl = [NSString stringWithFormat:@"/api/users/verify/%i?verificationCode=%@", [APIUserInfo sharedAPIUserInfo].apiUserIdx, [_verificationCode text]];
NSLog(@"the PUT url is %@", putUrl);
[[RKClient sharedClient] put:putUrl params:nil delegate:self];
我会告诉后端人。你能评论我应该使用哪种方法吗? – 2012-08-09 14:47:45
我不是一个休息的专家,但如果你打算使用休息,我想你只需要在服务器上启用PUT,你应该很好。有安全隐患,但你的后端人员将不得不与你一起工作。 – 2012-08-10 04:55:10
感谢您的帮助。我和他们交谈,他们发现他们正在解决的安全配置问题。至于__how__我认为方法2是正确的。当我得到最终的实施工作时,我会进行更新。 – 2012-08-10 15:20:49