【问题标题】:Flutter api showing 'Required parameter missing or invalid'Flutter api 显示“必需参数丢失或无效”
【发布时间】:2021-10-07 16:58:59
【问题描述】:

Flutter rest API 显示“必需参数丢失或无效”

main() async {      
  final data = {
    'customer': {
      'first_name': 'Karla',
      'last_name': 'Mullins',
      'email': 'KarlaMullins@mailinator.com',
      'phone': '+15142546011',
      'verified_email': true,
      'addresses': [
        {
          'address1': '123 Oak St1',
          'city': 'Ottawa1',
          'province': 'ON',
          'phone': '555-1212',
          'zip': '123 ABC',
          'last_name': 'Mullins',
          'first_name': 'Karla',
          'country': 'CA',
        }
      ]
    }
  };
  var json = jsonEncode(data);
  String username = 'u';
  String password = 'p';
  String basicAuth =
      'Basic ' + base64Encode(utf8.encode('$username:$password'));

  var response = await http.post(url',
    headers: <String, String>{
      'authorization': basicAuth,
      "Accept": "application/json",
    },
    body: json,
  );

  print('Response status: ${response.statusCode}');
  print('Response body: ${response.body}');

使用 Flutter dart 的基本授权调用 rest api,显示响应状态:400 和响应正文:{"errors":{"customer":"必需参数丢失或无效"}}。

【问题讨论】:

  • 检查后端开发或 api 文档并验证您是否将所有预期参数传递给后端
  • 我已经用相同的数据格式与邮递员核实过。
  • 无法为您提供帮助,因为我们无权访问 API 或不知道该 API 的实现逻辑。如果它在邮递员中工作,只需在两种情况下交叉检查请求。在这两种情况下检查身份验证标头。在您的代码中还有一个' 靠近url 参数

标签: flutter api rest


【解决方案1】:

Accept改成"content-type":"application/x-www-form-urlencoded

或应用程序/json,取决于您的 api。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2015-08-11
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多