设置请求头:[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
GET方法设置请求头的时候没什么问题;但是使用POST方法时设置无效,使用其他方法都没用,后来发现没有序列化
//AFJSONRequestSerializer 请求格式是JSON格式,AFHTTPRequestSerializer 请求格式是二进制
manager.requestSerializer = [AFJSONRequestSerializer serializer];
注意: 和manager.requestSerializer = [AFHTTPRequestSerializer serializer];不一样
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];manager.requestSerializer = [AFJSONRequestSerializer serializer];[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];[manager POST:url parameters:parametersDictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"success!"); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"error: %@", error); }];
参考: