首先,我意识到对于PUT请求,请求参数应该在请求体中传递。但是,我正在使用一个API(我只是消费,而不是开发),它希望将请求参数作为PUT请求的查询字符串附加。
我正在使用AFHTTPClient的子类。对于我所指的特定PUT请求,我使用 getPath:parameters:success:failure:。
到目前为止我找到的解决方案是手动构建路径变量以包含我想要传递的参数。当然,这并不理想,我想知道是否有其他选项不易出错。
简而言之,有没有办法使用AFHTTPClient(子类)发送PUT请求,并将附加(和编码)的传递参数作为查询字符串(就像GET请求一样)?
答案 0 :(得分:4)
getPath:参数:成功:AFHTTPClient.m内部的失败方法调用requestWithMethod:path:parameters。在后一种方法中,将针对某些值检查HTTP方法,以决定如何将参数附加到请求。如您所见,默认情况下,参数只应在GET,HEAD或DELETE请求的情况下附加到URL。因为在PUT请求的情况下你需要将它们附加到URL,所以修改requestWithMethod:path:参数如下:
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
{
NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[request setHTTPMethod:method];
[request setAllHTTPHeaderFields:self.defaultHeaders];
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"]) {
[request setHTTPShouldUsePipelining:YES];
}
if (parameters) {
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"] || [method isEqualToString:@"PUT"]) {
url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]];
[request setURL:url];
} else {
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding));
switch (self.parameterEncoding) {
case AFFormURLParameterEncoding:;
[request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]];
break;
case AFJSONParameterEncoding:;
[request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[AFJSONStringFromParameters(parameters) dataUsingEncoding:self.stringEncoding]];
break;
case AFPropertyListParameterEncoding:;
[request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[AFPropertyListStringFromParameters(parameters) dataUsingEncoding:self.stringEncoding]];
break;
}
}
}
return request;
}
答案 1 :(得分:3)
您可以在不修改底层AFNetworking代码的情况下执行datwalk答案中的代码。使用AFNetworking创建包含URL参数的路径:
NSDictionary *mutableParameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"csv",@"format", @"0",@"level", @"2013-10-25", @"keydate", nil];
NSString *urlPath = [NSString stringWithFormat:@"applications/%@/Planning?%@", name, AFQueryStringFromParametersWithEncoding(mutableParameters, NSUTF8StringEncoding)];
NSString *apiFunctionPath = [urlPath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self putPath:apiFunctionPath
parameters:nil
success:^(AFHTTPRequestOperation *operation, id XML) {
NSLog(@"%@",XML);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
答案 2 :(得分:1)
对于我的API,改变AFHTTPClient对象的编码解决了这个问题:
myAFHTTPClient.parameterEncoding = AFJSONParameterEncoding;