【发布时间】: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