Home > Software design >  Flutter http.dart post request to Infura Ipfs API Response status: 400 Response body: file argument
Flutter http.dart post request to Infura Ipfs API Response status: 400 Response body: file argument

Time:11-30

I'm trying to make a post request to /api/v0/add but the server respond with the following

error message

and this is the request code:


 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}');

I have olso tryed to parse the body with a string but nothing changed.

the api on postman works

api postman

CodePudding user response:

In your Postman screenshot, the radio button for "Form Data" is selected. This is plain old form encoded data, yet you've JSON-encoded your map unnecessarily.

Change your code to this:

  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',
    },
  );

CodePudding user response:

Add json content type header.

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

  • Related