今天對了個接口,正常用postman測試厂庇,form-data報錯400渠啊,于是改為json形式,通了权旷,所以代碼里面也得改
AFN代碼里AFURLRequestSerialization.m文件第104行替蛉,改源碼
- (NSString *)URLEncodedStringValue {
if (!self.value || [self.value isEqual:[NSNull null]]) {
return AFPercentEscapedStringFromString([self.field description]);
} else {
return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedStringFromString([self.field description]), AFPercentEscapedStringFromString([self.value description])];
}
}
改成
- (NSString *)URLEncodedStringValue {
if (!self.value || [self.value isEqual:[NSNull null]]) {
return AFPercentEscapedStringFromString([self.field description]);
}else if(!self.field || [self.field isEqual:[NSNull null]]){
return [NSString stringWithFormat:@"%@",self.value];
}else {
return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedStringFromString([self.field description]), AFPercentEscapedStringFromString([self.value description])];
}
}
然后在使用的時候?qū)⒆值湫问降膮?shù)轉(zhuǎn)成字符串
NSString* jsonStr = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:paramsDict options:0 error:nil] encoding:NSUTF8StringEncoding];
請求頭里面將Content-Type對應(yīng)值為application/json
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
嗯,收工