如何解决 AFNetworking 中的 header 问题?
How to solve header issue in AFNetworking?
数据在 Postman 应用程序中运行良好,但在代码中运行不正常。下面是代码
NSString* link = [NSString stringWithFormat:@"http://apifm.azurewebsites.net/token"];
AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:nil];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:link parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSMutableData *email = [[NSMutableData alloc] init];
NSMutableData *password = [[NSMutableData alloc] init];
NSMutableData *grantType = [[NSMutableData alloc] init];
[email appendData:[[NSString stringWithFormat:@"testman1@foodmandu.com"] dataUsingEncoding:NSUTF8StringEncoding]];
[password appendData:[[NSString stringWithFormat:@"testUserPw"] dataUsingEncoding:NSUTF8StringEncoding]];
[grantType appendData:[[NSString stringWithFormat:@"password"] dataUsingEncoding:NSUTF8StringEncoding]];
[formData appendPartWithFormData:email name:@"UserName"];
[formData appendPartWithFormData:password name:@"Password"];
[formData appendPartWithFormData:grantType name:@"grant_type"];
} success:^(AFHTTPRequestOperation* operation, id responseObject) {
NSLog(@"success");
} failure:^(AFHTTPRequestOperation* operation, NSError* error){
[API alertViewWithMessage:[[operation responseObject] objectForKey:@"error"] inController:self];
}];
以下是Postman app的截图
下面给出了我在执行代码时遇到的错误
error Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7faf3b6d6cf0 {com.alamofire.serialization.response.error.response= { URL: http://apifm.azurewebsites.net/token } { status code: 400, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 34;
"Content-Type" = "application/json;charset=UTF-8";
Date = "Sun, 05 Jul 2015 07:11:19 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/8.0";
"Set-Cookie" = "ARRAffinity=ebb02fb6b319b5ae83e167620f3bc3ad6e68d31fb30aff79ef123aabd981588e;Path=/;Domain=apifm.azurewebsites.net";
"X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://apifm.azurewebsites.net/token, com.alamofire.serialization.response.error.data=<7b226572 726f7222 3a22756e 73757070 6f727465 645f6772 616e745f 74797065 227d>, NSLocalizedDescription=Request failed: bad request (400)}
提醒您,数据在 Postman 中运行良好。
您应该将参数作为 NSDictionary 传递,例如“NSDictionary *parameters = @{@"UserName": @"testman1@foodmandu.com", @"Password": @"testUserPw", @ "grant_type": @"password"}; "
这可以使用以下代码实现:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"UserName": @"testman1@foodmandu.com", @"Password": @"testUserPw", @"grant_type": @"password"};
[manager POST:@"http://apifm.azurewebsites.net/token" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
请尝试以上操作,并让我知道您的反馈。
数据在 Postman 应用程序中运行良好,但在代码中运行不正常。下面是代码
NSString* link = [NSString stringWithFormat:@"http://apifm.azurewebsites.net/token"];
AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:nil];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:link parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSMutableData *email = [[NSMutableData alloc] init];
NSMutableData *password = [[NSMutableData alloc] init];
NSMutableData *grantType = [[NSMutableData alloc] init];
[email appendData:[[NSString stringWithFormat:@"testman1@foodmandu.com"] dataUsingEncoding:NSUTF8StringEncoding]];
[password appendData:[[NSString stringWithFormat:@"testUserPw"] dataUsingEncoding:NSUTF8StringEncoding]];
[grantType appendData:[[NSString stringWithFormat:@"password"] dataUsingEncoding:NSUTF8StringEncoding]];
[formData appendPartWithFormData:email name:@"UserName"];
[formData appendPartWithFormData:password name:@"Password"];
[formData appendPartWithFormData:grantType name:@"grant_type"];
} success:^(AFHTTPRequestOperation* operation, id responseObject) {
NSLog(@"success");
} failure:^(AFHTTPRequestOperation* operation, NSError* error){
[API alertViewWithMessage:[[operation responseObject] objectForKey:@"error"] inController:self];
}];
以下是Postman app的截图
下面给出了我在执行代码时遇到的错误
error Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7faf3b6d6cf0 {com.alamofire.serialization.response.error.response= { URL: http://apifm.azurewebsites.net/token } { status code: 400, headers { "Cache-Control" = "no-cache"; "Content-Length" = 34; "Content-Type" = "application/json;charset=UTF-8"; Date = "Sun, 05 Jul 2015 07:11:19 GMT"; Expires = "-1"; Pragma = "no-cache"; Server = "Microsoft-IIS/8.0"; "Set-Cookie" = "ARRAffinity=ebb02fb6b319b5ae83e167620f3bc3ad6e68d31fb30aff79ef123aabd981588e;Path=/;Domain=apifm.azurewebsites.net"; "X-Powered-By" = "ASP.NET"; } }, NSErrorFailingURLKey=http://apifm.azurewebsites.net/token, com.alamofire.serialization.response.error.data=<7b226572 726f7222 3a22756e 73757070 6f727465 645f6772 616e745f 74797065 227d>, NSLocalizedDescription=Request failed: bad request (400)}
提醒您,数据在 Postman 中运行良好。
您应该将参数作为 NSDictionary 传递,例如“NSDictionary *parameters = @{@"UserName": @"testman1@foodmandu.com", @"Password": @"testUserPw", @ "grant_type": @"password"}; "
这可以使用以下代码实现:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"UserName": @"testman1@foodmandu.com", @"Password": @"testUserPw", @"grant_type": @"password"};
[manager POST:@"http://apifm.azurewebsites.net/token" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
请尝试以上操作,并让我知道您的反馈。