【问题标题】:How to solve header issue in AFNetworking?如何解决 AFNetworking 中的标头问题?
【发布时间】:2015-09-22 13:07:39
【问题描述】:

数据在 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 应用的截图

我在执行代码时遇到的错误如下所示

错误错误域=com.alamofire.error.serialization.response Code=-1011“请求失败:错误请求(400)” UserInfo=0x7faf3b6d6cf0 {com.alamofire.serialization.response.error.response= { URL:@ 987654321@ } {状态码:400,标题{ “缓存控制”=“无缓存”; “内容长度”= 34; “内容类型”=“应用程序/json;字符集=UTF-8”; 日期 =“星期日,2015 年 7 月 5 日 07:11:19 GMT”; 过期=“-1”; Pragma = "无缓存"; 服务器 = "Microsoft-IIS/8.0"; "Set-Cookie" = "ARRAffinity=ebb02fb6b319b5ae83e167620f3bc3ad6e68d31fb30aff79ef123aabd981588e;路径=/;域=apifm.azurewebsites.net"; "X-Powered-By" = "ASP.NET"; } }, NSErrorFailingURLKey=http://apifm.azurewebsites.net/token, com.alamofire.serialization.response.error.data=, NSLocalized

提醒您,数据在 Postman 中运行良好。

【问题讨论】:

  • 我在邮递员上检查了这个,我得到的回复是“unsupported_grant_type”,你能确认你的完整帖子网址看起来像这样apifm.azurewebsites.net/…
  • @Max 是的,我刚刚确认了。另外,您可以在我的最后一个问题中的错误日志中看到apifm.azurewebsites.net/token

标签: ios objective-c afnetworking-2


【解决方案1】:

您应该将参数作为 NSDictionary 传递,例如 " NSDictionary *parameters = @{@"UserName": @"testman1@foodmandu.com", @"Password": @"testUserPw", @"grant_type": @"密码"}; "

这可以使用以下代码来实现:

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);
}]; 

请尝试以上方法并告诉我您的反馈。

【讨论】:

    猜你喜欢
    • 2019-11-08
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多