RestKit PUT无法正常工作

时间:2012-08-09 01:25:18

标签: objective-c ios restkit http-put

我正在尝试使用RestKit进行相当基本的HTTP PUT。我不想放置整个对象,因为API调用旨在接受单个查询参数并只更新该字段。到目前为止,我尝试了两种方法,都不成功。

发布到的网址:https://myserver/api/users/{userId}

查询字符串参数:verificationCode=

使用示例:PUT https://myserver/api/users/101?verificationCode=646133

方法#1:将查询参数放在RKParams对象中并使用这些参数进行PUT调用。

    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:构建整个网址并尝试将参数设置为nil的PUT。

    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示例吗?我在那里找到的大多数例子都是把整个对象放在我不想做的事情上。


更新

方法#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];

1 个答案:

答案 0 :(得分:1)

您的网络服务器上禁用了HTTP PUT方法。出于安全原因,默认情况下它位于所有Web服务器上。

HTTP Status 405 - Method Not Allowed.
相关问题