【问题标题】:how to post form data request using flutter in http package如何在http包中使用flutter发布表单数据请求
【发布时间】:2020-10-14 05:30:31
【问题描述】:

我想使用 HTTP 包发送表单数据。得到错误:FormatException: Unexpected character (at character 1) 我/颤振(30465): 我在 HTTP 发布请求中发送表单数据。

Future<void> authethicate(
    String schoolName,
    String password,
  ) async {
    try {
      final url = 'https://yobimx.com/citykey/api/users/login';
      final response = await http.post(url, body: {
        'email': 'usamashafiq199@outlook.com',
        'password': '123',
      }, headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      });
      print(
        json.decode(response.body),
      );
      final responseData = json.decode(response.body);
    } catch (error) {
      print(error);
    }
  }

【问题讨论】:

  • 如果你删除 json.decode 并打印 response.body,你会得到什么打印结果?

标签: flutter http dart rest


【解决方案1】:

我必须对请求使用多部分请求。感谢您的帮助。

Future<void> authethicate(
    String schoolName,
    String password,
  ) async {
    try {
      final url = Uri.parse('https://yobimx.com/citykey/api/users/login');
      Map<String, String> requestBody = <String, String>{
        'email': 'usamashafiq199@outlook.com',
        'password': '123'
      };
      var request = http.MultipartRequest('POST', url)
        ..fields.addAll(requestBody);
      var response = await request.send();
      final respStr = await response.stream.bytesToString();
      print(
        jsonDecode(respStr),
      );
      print("This is the Status Code$respStr");
      var encoded = json.decode(respStr);

      print(encoded['status']);
      print('This is the userId${encoded['data']['user_id']}');
    } catch (error) {
      print(error);
    }
  }

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    相关资源
    最近更新 更多