json - Flutter发送post请求返回http错误500

标签 json flutter http

我一直在研究 flutter 项目,该项目接受用户的姓名和电话号码,但是当我保存它时,它显示响应 500,但从 postman 那里它工作正常。 enter image description here

但是来自 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/

相关文章:

json - 如何在同一条消息中多次循环不同类型的嵌套 JSON 对象

listview - Flutter ListView base64图像在使用Image.memory显示时闪烁

javascript - 每当客户端请求页面时返回 URL

php - https 和来自另一个站点的图像。如何在不修改 .htaccess 文件的情况下使用 php 或 javascript 删除 http 图像的警告?

jquery - 单个对象绑定(bind)但列表集合不绑定(bind)

jquery - 将 JSON 从操作类传递到 JSP,而不在 struts 中使用 Ajax

android - 使用 Flutter,在低于 20 的 Android 版本中运行谷歌地图时,它不起作用..有什么想法吗?

list - 在 Dart 中显示没有重复项的列表 <Map>

HTTP 套接字保持事件状态不起作用

ios - 是否可以为纯 k-v json 创建 Swift Codable?