Flutter http.dart向Infura Ipfs API发送请求响应状态:400响应正文:需要文件参数“path”

h43kikqp  于 2022-11-30  发布在  Flutter
关注(0)|答案(2)|浏览(186)

我尝试向/api/v0/add发出post请求,但服务器响应如下
error message
这是请求代码:

String basicAuth = 'Basic ${base64.encode(utf8.encode("$username:$password"))}';
        
 final Map body = {'file': '$path/light.txt'};

        var url = Uri.https(
            'ipfs.infura.io:5001',
            '/api/v0/add'
        );
        print(url);
        var response = await http.post(
          url,
          body: json.encode(body),
            headers: <String, String>{
             "Authorization": basicAuth,
           }
        );
        print('REQUEST: ${response.request}');
        print('Response status: ${response.statusCode}');
        print('Response body: ${response.body}');

我也尝试过用字符串解析主体,但没有任何变化。
有关 Postman 工作的宣传短片
api postman

sgtfey8w

sgtfey8w1#

在Postman屏幕截图中,选中了“Form Data”单选按钮。这是一个简单的旧表单编码数据,但您对Map进行了不必要的JSON编码。
将代码更改为:

final auth = base64.encode(utf8.encode('$username:$password'));
  final body = <String, String>{'file': '$path/light.txt'};

  final response = await http.post(
    Uri.https('ipfs.infura.io:5001', '/api/v0/add'),
    body: body,
    headers: <String, String>{
      'Authorization': 'Basic $auth',
    },
  );
nwlls2ji

nwlls2ji2#

添加json内容类型标头。

headers: {
  ...
  'Content-Type': 'application/json; charset=UTF-8',
},

相关问题