0

Im trying to use an API to remove background of an image. Therefore, I am using http flutter package. But I am getting <title>400 Bad Request</title> error. How can I fix this?

This is what i tried,

  removeBackGround(String imagePath) async {
    
    final body = {"image_file": imagePath};
    final headers = {"X-API-Key": "e5adebcdc77**************"};
    final response = await http.post(Uri.parse("https://sdk.photoroom.com/v1/segment"),
        body: body,
        headers: headers);

    if (response.statusCode == 200) {
      print("response  ok");
      
    } else {
      throw Exception('Failed to do network requests: Error Code: ${response.statusCode}\nBody: ${response.body}');
    }
  }

this is the error

E/flutter (14230): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Exception: Failed to do network requests: Error Code: 400
E/flutter (14230): Body: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
E/flutter (14230): <title>400 Bad Request</title>
E/flutter (14230): <h1>Bad Request</h1>
E/flutter (14230): <p>Could not decode image, ensure image_file or image_file_b64 is set</p>
3
  • 1
    "Could not decode image, ensure image_file or image_file_b64 is set" have you tried converting the image into base64? Or to convert into a File object? You're just passing a path Commented Jun 19, 2021 at 13:27
  • @MarianoZorrilla I just passed File directly instead of the path and I am getting this error now Error - '[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type '_File' is not a subtype of type 'String' in type cast' Commented Jun 19, 2021 at 13:38
  • No! @MarianoZorrilla you can upload an image from the path too. Commented Jun 19, 2021 at 14:03

1 Answer 1

1

You can use the post method slightly in a different way, by using MulipartFormRequest

Future removeBackGround(imagePath) async {
  var request = http.MultipartRequest(
      'POST', Uri.parse("https://sdk.photoroom.com/v1/segment"));
  await http.MultipartFile.fromPath(
    'image',
    imagePath,
    filename: 'image_$name.jpg',
  );
  // request.fields['companyName'] = name;
  // request.fields['own_id'] = id;
  request.headers["X-API-Key"] = "e5adebcdc77**************";

  var res = await request.send();
  return res.statusCode;
}

Using this method you can upload an images and json data at once.

Sign up to request clarification or add additional context in comments.

15 Comments

What should I use as$name? random name or the exact name of the image? [image_$name.jpg]
It's completely up to you, whatever you want,
I use this just for demo purpose image_$name.jpg can give any name. Its better you Use baseName from imagepicker
Bro, I am still getting code 400 :( Why is that? Please help me.
The problem is they want a binary image and you are try to send them an image file. so they couldn't understand what we send to them
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.