AFNetworking 不支持 text/plain,unacceptable content-type: text/plain

本文详细介绍了在使用AFNetworking进行POST请求以获取微博accessToken时遇到的unacceptable content-type错误问题,并提供了两种解决方案。通过在头文件中添加text/plain或初始化AFNetworking响应序列化器,可以解决此问题。同时,文章还提供了关键代码片段作为参考。

1、 用AFNetworkingPOST传递参数(获取微博的accessToken)的时候,具体代码如下:

AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"client_id"] = @"3113926977";
    parameters[@"client_secret"] = @"09c7301ac169533b1f2440a018e6ce93";
    parameters[@"grant_type"] = @"authorization_code";
    parameters[@"code"] = code;
    parameters[@"redirect_uri"] = @"http://www.baidu.com";
    
    [session POST:@"https://api.weibo.com/oauth2/access_token" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

报,警告:"Request failed: unacceptable content-type: text/plain"


百度出来说AFNetworking不支持text/plain,这个格式。

解决办法1.:在头文件"AFURLResponseSerialization.m"的如下地方添加text/plain就行了。问题解决。

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain",@"text/json", @"text/javascript", nil];

 解决办法2.添加一句代码:

session.responseSerializer = [[AFCompoundResponseSerializer alloc] init];

问题也能解决,但是这样返回的的data数据,还是需要自己转成需要的格式。

转载于:https://www.cnblogs.com/XXxiaotaiyang/p/5051142.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值