PUT请求使用ASIHTTP?

时间:2014-02-19 11:38:52

标签: ios objective-c web-services rest asihttprequest

我有一个Web服务,它使用PUT方法从URL default / isUserInternational / id / 1请求,并且在请求正文中应该追加isInternational = 1.

我正在使用以下代码但仍无法发出PUT请求。

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"default/isUserInternational/id/%@", @"5"]];
self.asiFormDataRequest = [[ASIFormDataRequest alloc] initWithURL:url];
[self.asiFormDataRequest setDelegate:self];
[self.asiFormDataRequest setTimeOutSeconds:REQUEST_TIMEOUT];
[self.asiFormDataRequest setPostFormat:ASIMultipartFormDataPostFormat];
[self.asiFormDataRequest setRequestMethod:@"PUT"];
[self.asiFormDataRequest addRequestHeader:@"Accept" value:@"application/json"];
[self.asiFormDataRequest addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];

//[self.asiFormDataRequest addRequestHeader:@"1" value:@"isInternational"];
//[self.asiFormDataRequest appendPostData:[@"1" dataUsingEncoding:NSUTF8StringEncoding]];

[self.asiFormDataRequest addPostValue:@"1" forKey:@"isInternational"];
[self.asiFormDataRequest startSynchronous];

2 个答案:

答案 0 :(得分:0)

如果您想通过PUT发送数据,请使用appendPostData:appendPostDataFromFile:

答案 1 :(得分:0)

虽然你使用了appendPostData:或appendPostDataFromFile:它不会工作。另外,如果您使用它们,它将被视为文档中提到的POST请求。

//当你使用appendPostData时,默认变为POST:/ appendPostDataFromFile:/ setPostBody:

http://allseeing-i.com/ASIHTTPRequest/How-to-use#setting_request_headers)。

所以我发布了对我有用的正确代码,希望它对其他人有用!

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"default/isUserInternational/id/%@", @"5"]];
self.asiFormDataRequest = [[ASIFormDataRequest alloc] initWithURL:url];
[self.asiFormDataRequest setDelegate:self];
[self.asiFormDataRequest setTimeOutSeconds:REQUEST_TIMEOUT];
//[self.asiFormDataRequest setPostFormat:ASIMultipartFormDataPostFormat];
[self.asiFormDataRequest addRequestHeader:@"Accept" value:@"application/json"];
[self.asiFormDataRequest addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[self.asiFormDataRequest addRequestHeader:@"1" value:@"isInternational"];
[self.asiFormDataRequest setRequestMethod:@"PUT"];

[self.asiFormDataRequest addPostValue:@"1" forKey:@"isInternational"];
[self.asiFormDataRequest startSynchronous];

AND REMEMBER删除行[self.asiFormDataRequest setPostFormat:ASIMultipartFormDataPostFormat];因为它使您的请求被视为POST。这是我的愚蠢错误!