Flutter 发送空报头的颤振http post方法
我试图将formdata发布到服务器,但Http.post方法正在发送空的头。当我打印数据和标题时,api在postman中也可以正常工作,但在实际设备中却不能。我已实施以下措施:Flutter 发送空报头的颤振http post方法,flutter,flutter-dependencies,Flutter,Flutter Dependencies,我试图将formdata发布到服务器,但Http.post方法正在发送空的头。当我打印数据和标题时,api在postman中也可以正常工作,但在实际设备中却不能。我已实施以下措施: Future<CommonResponse> postRecharge( String telId, String typeId, String money, String phone) async { SharedPreferences sharedPreferences = awa
Future<CommonResponse> postRecharge(
String telId, String typeId, String money, String phone) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString("apiToken");
Map data = {
'telecommunication_id': telId,
'telecommunication_type_id': typeId,
'money_sent': money,
'receiver_mobile': phone,
};
Map<String, String> header = {"Authorization": '$token'};
var jsonResponse;
CommonResponse commonResponse;
var response = await http.post(recharges, headers: header, body: data);
print(response.statusCode);
if (response.statusCode == 200) {
jsonResponse = json.decode(response.body);
if (jsonResponse != null) {
commonResponse = new CommonResponse.fromJson(jsonResponse);
print(commonResponse);
return commonResponse;
} else {
return null;
}
} else {
jsonResponse = json.decode(response.body.toString());
print(jsonResponse);
commonResponse = new CommonResponse.fromJson(jsonResponse);
return commonResponse;
}
}
Http依赖项:
http: ^0.12.2
我实施了如下post方法:
Future<CommonResponse> postRecharge(
String telId, String typeId, String money, String phone) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString("apiToken");
Map data = {
'telecommunication_id': telId,
'telecommunication_type_id': typeId,
'money_sent': money,
'receiver_mobile': phone,
};
Map<String, String> header = {"Authorization": '$token'};
var jsonResponse;
CommonResponse commonResponse;
var response = await http.post(recharges, headers: header, body: data);
print(response.statusCode);
if (response.statusCode == 200) {
jsonResponse = json.decode(response.body);
if (jsonResponse != null) {
commonResponse = new CommonResponse.fromJson(jsonResponse);
print(commonResponse);
return commonResponse;
} else {
return null;
}
} else {
jsonResponse = json.decode(response.body.toString());
print(jsonResponse);
commonResponse = new CommonResponse.fromJson(jsonResponse);
return commonResponse;
}
}
未来后充电(
字符串telId、字符串typeId、字符串money、字符串phone)异步{
SharedReferences SharedReferences=等待SharedReferences.getInstance();
String token=SharedReferences.getString(“apiToken”);
地图数据={
“电信id”:telId,
“电信类型id”:类型id,
“寄钱”:钱,
“手机接收器”:手机,
};
映射头={“授权”:“$token”};
var-jsonResponse;
共同反应共同反应;
var response=wait http.post(充值,头:头,体:数据);
打印(响应状态码);
如果(response.statusCode==200){
jsonResponse=json.decode(response.body);
if(jsonResponse!=null){
commonResponse=newCommonResponse.fromJson(jsonResponse);
打印(公共响应);
返回公共响应;
}否则{
返回null;
}
}否则{
jsonResponse=json.decode(response.body.toString());
打印(jsonResponse);
commonResponse=newCommonResponse.fromJson(jsonResponse);
返回公共响应;
}
}