如何使用 body 和 URL 中的参数制作经过身份验证的 POST?
How to make an authenticated POST with parameters in body and in URL?
由于服务器限制,我需要制作一个 POST
,其中包含 URL 和 body 中的参数。我正在使用 AFHTTPRequestOperationManager
的 HTTPRequestOperationWithRequest
方法来完成此操作,因为我可以使用 URL 参数初始化 URL 请求,例如:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:components.URL];
然后我可以分别设置 POST
的 body 像:
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
当我将 AFHTTPRequestOperationManager
的 requestSerializer
设置为使用授权 header 令牌时,问题就出现了。请求返回为 401 unauth
。但是,如果我直接将请求 object 的 header 设置为:
[request setValue:self.accessToken forHTTPHeaderField:@"Authorization"];
调用成功。任何人都知道更好的方法来进行经过身份验证的调用,同时在 POST
的 body 和 URL 中发送参数?我的实施似乎不太理想。
除非您需要支持 iOS6,否则我认为您应该改用 AFHTTPSessionManager 而不是 AFHTTPRequestOperationManager。
子类 AFHTTPSessionManager 并覆盖:
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
uploadProgress:(void (^)(NSProgress * _Nonnull))uploadProgressBlock
downloadProgress:(void (^)(NSProgress * _Nonnull))downloadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler {
NSMutableURLRequest *modifiedRequest = request.mutableCopy;
NSString *token = [Get your access token];
[modifiedRequest addValue:token forHTTPHeaderField:@"Authorization"];
// Now set up the data task as normal
return [super dataTaskWithRequest:modifiedRequest
uploadProgress:uploadProgressBlock
downloadProgress:downloadProgressBlock
completionHandler:completionHandler];
}
它的结果与您的方法相同(我不知道有更好的方法)- 但确保它会自动发生在所有调用中。
将会话管理器初始化为子类的实例。
然后使用 AFNetworking 的 POST 方法,设置您的参数并执行:
NSDictionary *params = @{@"Param1":param1 ? param1 : @"",
@"Param2":param2 ? param2 : @""};
[self.sessionManager
POST:self.serviceEndpointUrl
parameters:params
etc..
由于服务器限制,我需要制作一个 POST
,其中包含 URL 和 body 中的参数。我正在使用 AFHTTPRequestOperationManager
的 HTTPRequestOperationWithRequest
方法来完成此操作,因为我可以使用 URL 参数初始化 URL 请求,例如:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:components.URL];
然后我可以分别设置 POST
的 body 像:
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
当我将 AFHTTPRequestOperationManager
的 requestSerializer
设置为使用授权 header 令牌时,问题就出现了。请求返回为 401 unauth
。但是,如果我直接将请求 object 的 header 设置为:
[request setValue:self.accessToken forHTTPHeaderField:@"Authorization"];
调用成功。任何人都知道更好的方法来进行经过身份验证的调用,同时在 POST
的 body 和 URL 中发送参数?我的实施似乎不太理想。
除非您需要支持 iOS6,否则我认为您应该改用 AFHTTPSessionManager 而不是 AFHTTPRequestOperationManager。
子类 AFHTTPSessionManager 并覆盖:
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
uploadProgress:(void (^)(NSProgress * _Nonnull))uploadProgressBlock
downloadProgress:(void (^)(NSProgress * _Nonnull))downloadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler {
NSMutableURLRequest *modifiedRequest = request.mutableCopy;
NSString *token = [Get your access token];
[modifiedRequest addValue:token forHTTPHeaderField:@"Authorization"];
// Now set up the data task as normal
return [super dataTaskWithRequest:modifiedRequest
uploadProgress:uploadProgressBlock
downloadProgress:downloadProgressBlock
completionHandler:completionHandler];
}
它的结果与您的方法相同(我不知道有更好的方法)- 但确保它会自动发生在所有调用中。
将会话管理器初始化为子类的实例。
然后使用 AFNetworking 的 POST 方法,设置您的参数并执行:
NSDictionary *params = @{@"Param1":param1 ? param1 : @"",
@"Param2":param2 ? param2 : @""};
[self.sessionManager
POST:self.serviceEndpointUrl
parameters:params
etc..