以下是我的Http发布请求,它没有在正文中发送我的表单数据,并且在邮递员中正常工作。
它只需要我现在传递静态的正文中的电子邮件参数,但仍然,我认为我的正文发送为空,请提出任何建议。

   Future<Map> getJson() async {
    String apiUrl = 'this is my url';

    Map<String, String> headers = {
      'Content-Type': 'application/json',
      'Authorization':'this is my security token'

    };
    final msg = jsonEncode({
      "email":"[email protected]",
    });

    http.Response response = await http.post(
      apiUrl,
      headers: headers,
      body: msg,
    );
    return json.decode(response.body); // returns a List type
  }

  void check() async{
Map _data = await getJson();
 print("$_data");
  }


  @override
  void initState() {
    super.initState();
    check();
  }

邮递员回应
{
    "status": "SUCCESS",
    "msg": {
        "msg": "Password email reset code has been sent in email. Code will get expire after 24 hours!"
    },
    "code": 446167
}

我的代码回应
{status: ERROR, data: {msg: fields are required}}

最佳答案

如果您发布表单数据,请使用此代码

var uri = Uri.parse('https://example.com/create');

var request = http.MultipartRequest('POST', uri)
  ..fields['email'] = '[email protected]';

  request.headers.addAll({
      'Content-Type': 'multipart/form-data',
      'Authorization':'your token'
    });


var response = await request.send();
if (response.statusCode == 200) print('Done!');

关于api - 我 flutter 的Http发布请求未发送表单数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62215138/

10-09 03:21