gpt4 book ai didi

ios - 如何解决 AFNetworking 中的 header 问题?

转载 作者:行者123 更新时间:2023-11-28 21:43:23 25 4
gpt4 key购买 nike

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

Body

下面给出了我在执行代码时遇到的错误

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": @"密码"};"

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

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

请尝试以上操作并让我知道您的反馈。

关于ios - 如何解决 AFNetworking 中的 header 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31228010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com