我需要从我的颤振应用程序中向API发出一个GET请求,该应用程序要求请求体为JSON (raw)。
我在Postman中用JSON请求体测试了API,它似乎运行得很好。
现在,在我的颤振应用程序中,我试图做同样的事情:
_fetchDoctorAvailability() async {
var params = {
"doctor_id": "DOC000506",
"date_range": "25/03/2019-25/03/2019" ,
"clinic_id":"LAD000404"
};
Uri uri = Uri.parse("http://theapiiamcalling:8000");
uri.replace(queryParameters: params);
var response = await http.get(uri, headers: {
"Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY,
HttpHeaders.contentTypeHeader: "application/json",
"callMethod" : "DOCTOR_AVAILABILITY"
});
print('---- status code: ${response.statusCode}');
var jsonData = json.decode(response.body);
print('---- slot: ${jsonData}');
}
然而,API给了我一个错误,说
{消息:缺少输入json.,状态: false}
如何为Http请求发送原始(或者更确切地说是JSON)请求体?
发布于 2021-08-26 09:51:36
如果您想通过下面的示例通过GET请求发送复杂/嵌套的数据,可以使用我在github https://github.com/opatajoshua/SimplifiedUri上创建的一个简单类
final params = {
'name': 'John',
'columns': ['firstName', 'lastName'],
'ageRange': {
'from': 12,
'to': 60,
},
'someInnerArray': [1,2,3,5]
};
final Uri uri = SimplifiedUri.uri('http://api.mysite.com/users', params);
final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
final response = await http.get(uri, headers: headers);
输出
http://api.mysite.com/users?name=John&columns%5B%5D=firstName&columns%5B%5D=lastName&ageRange%5Bfrom%5D=12&ageRange%5Bto%5D=60&someInnerArray%5B%5D=1&someInnerArray%5B%5D=2&someInnerArray%5B%5D=3&someInnerArray%5B%5D=5
https://stackoverflow.com/questions/55331782
复制相似问题