AFNetworking的body如何传json类型
parameters加入body中,UTF-8格式
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//发送类型(二进制格式)
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//接收类型(二进制格式)
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//超时时间
manager.requestSerializer.timeoutInterval = 20.0;
[manager PUT:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//查看body内容,data转string
if(task.originalRequest.HTTPBody != nil){
NSString *str = [[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:kCFStringEncodingUTF8];
NSLog(@"成功,打印httpBody:%@", str);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//查看body内容,data转string
if(task.originalRequest.HTTPBody != nil){
NSString *str = [[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:kCFStringEncodingUTF8];
NSLog(@"成功,打印httpBody:%@", str);
}
}];
parameters加入body中,JSON格式
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//发送类型(JSON格式)
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//接收类型(二进制格式)
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//超时时间
manager.requestSerializer.timeoutInterval = 20.0;
//写与不写对最终结果不造成影响
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//写与不写对最终结果不造成影响
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager PUT:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//查看body内容,json解析
if(task.originalRequest.HTTPBody != nil){
NSString *str = [NSJSONSerialization JSONObjectWithData:task.originalRequest.HTTPBody options:0 error:nil];
NSLog(@"成功,打印httpBody:%@", str);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//查看body内容,json解析
if(task.originalRequest.HTTPBody != nil){
NSString *str = [NSJSONSerialization JSONObjectWithData:task.originalRequest.HTTPBody options:0 error:nil];
NSLog(@"成功,打印httpBody:%@", str);
}
}];
附:一种错误的写法
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//发送类型(二进制格式)
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//接收类型(二进制格式)
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//超时时间
manager.requestSerializer.timeoutInterval = 20.0;
//这里一旦多加这句,就会造成400错误。数据解析失败。AFNetworking没法正常解析需发送的body
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager PUT:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//查看body内容,data转string
if(task.originalRequest.HTTPBody != nil){
NSString *str = [[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:kCFStringEncodingUTF8];
NSLog(@"成功,打印httpBody:%@", str);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//查看body内容,data转string
if(task.originalRequest.HTTPBody != nil){
NSString *str = [[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:kCFStringEncodingUTF8];
NSLog(@"成功,打印httpBody:%@", str);
}
}];