我一直在研究 flutter 项目,该项目接受用户的姓名和电话号码,但是当我保存它时,它显示响应 500,但从 postman 那里它工作正常。
但是来自 flutter 这是代码
void RegisterUsers(String names, String phone) async{
String urlInser = "https://hmmmiii.com/api/addUser";
Map<String, String> headers = {
'Content-Type': 'application/json',
//'Accept': 'application/json',
};
final msg = jsonEncode({"Name":"$names","PhoneNumber":"$phone"});
var response = await http.post(urlInser,
headers: headers,
body: msg);
print(response.statusCode);
if (response.statusCode == 200) {
print('User Registered');
} else {
print('Unsuccessful');
}
其中姓名和电话是 textController 值,谢谢。
最佳答案
在 postman 中,您将请求作为表单数据发送,但在代码中,您将其作为简单的 JSON 发送。
您必须使用MultipartRequest
。
final url = 'your url';
final request = http.MultipartRequest('POST', Uri.parse(url))
..fields['Name'] = 'some name'
..fields['PhoneNumber'] = 'some phonenumber';
final response = await request.send();
final responseBody = await response.stream.bytesToString();
print('RESPONSE BODY: $responseBody');
关于json - Flutter发送post请求返回http错误500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65797760/