Background removal

FREEMIUM
Par ObjectCut API | Mise à jour 23 days ago | Video, Images
Popularité

9.4 / 10

Latence

6,960ms

Niveau de service

100%

Health Check

100%

Retour à toutes les discussions

400 BadRequest

Rapid account: Dylan Ccq 3 Vz Krs
dylan-ccq3vzKrs
3 years ago

No matter what i try i always get a BadRequest or if i try the C# example code i get UriString to long errors. Passing a base64 image through the query string is a really weird concept in my opinion.

If i post it as x-www-form-urlencoded i get 400, if i post it as json i get 400… Any tips are welcome.

Rapid account: Gokupc 21
gokupc21 Commented a year ago
"message": "'image' is a required property",
"param": null,
"type": "invalid_request_error"

}
same errror in flutter tried json encode decode

Future<String> removeBG(String imageUrl) async {
final response = await http.post(
Uri.parse(‘https://background-removal.p.rapidapi.com/remove’),
headers: {
“content-type”: “application/x-www-form-urlencoded”,
“X-RapidAPI-Key”: “–”,
“X-RapidAPI-Host”: “background-removal.p.rapidapi.com

  },
  encoding: Encoding.getByName('utf-8'),
  body: {"image_url": "https://objectcut.com/assets/img/raven.jpg"}
);

if (response.statusCode == 200) {
  final data = jsonDecode(response.body);
  print(response.body);
  print(response.headers);
  final imageURL = data['data']['image_url'];
  return imageURL;
} else {
  print(response.body);
  print(response.headers);
  throw Exception('Failed to remove background');
}

}

Rapid account: Objectcut Api
objectcut.api Commented 3 years ago

Hi Dylan,

Sorry for our late reply, we haven’t get any notification from this discussion thread.

I think the problem, based on the piece of code attached, that you are trying the send the image itself without any encoding (so, the image in bytes). Our API only supports, at least for now, the way of sending an image using a public URL or encoding it in base64 and sending over as a string.

The process that you should follow in case you want to use ObjectCut is to get the file that you want to process, encode it in base64 and sending that using the image_base64 form field as a string. If you send the image itself (in bytes) the API is gonna return you a 400 status code because it doesn’t fit with the input requirements.

I found this StackOverflow post that you could be useful for your integration: https://stackoverflow.com/questions/21325661/convert-an-image-selected-by-path-to-base64-string

If you are still having problems, let us know and we are gonna try to help you.

Thanks and sorry for the inconvenience.

Keep in touch.

Regards,
ObjectCut team.

Rapid account: Dylan Ccq 3 Vz Krs
dylan-ccq3vzKrs Commented 3 years ago

Hi,

Thank you for your reply.
I really didn’t get this to work with plain .NET Core. The example code complained about to long UriString or gave ‘BadRequest’ errors, without explanation.

I tried with an image of 800kb i think, maybe this is to large.

Anyway, after searching on google i ended up on https://imagezerow.com/#/ which is a different version of RapidAPI?
https://rapidapi.com/imagezero-imagezero-default/api/image-background-removal-v2/details

Here i am able to do a file upload with the following code.

` public static async Task<Bitmap> RemoveBackgroundFromImage(Bitmap bitmap)
{
var url = “rapidapi.url”;

        using (var stream = new MemoryStream())
        {
            bitmap.Save(stream, ImageFormat.Png);
            stream.Position = 0;

            var client = new HttpClient();
            var formData = new MultipartFormDataContent();
            formData.Add(new StreamContent(stream), "file", "file");
            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = formData
            };

            request.Headers.Add("accept", "application/json");

            var response = await client.SendAsync(request);
            if (response.IsSuccessStatusCode)
            {

                var result = System.Text.Json.JsonSerializer.Deserialize<Response>(await response.Content.ReadAsStringAsync());

                var bitmapStream = await client.GetStreamAsync(result.Result);
                bitmap = new Bitmap(bitmapStream);
                bitmapStream.Flush();
                bitmapStream.Close();
                stream.Close();

                return bitmap;
            }
            else
            {
                throw new Exception($"Could not remove background from image. the Result was: {response.StatusCode} - {response.ReasonPhrase}");

                return null;
            }
        }
    }
    public class Response
    {
        public string Result { get; set; }
    }`

If this same approach is possible with this API you can add my code as .NETCore C# version.

Rapid account: Objectcut Api
objectcut.api Commented 3 years ago

Hi Dylan,

Thanks for your question. I’m trying to reproduce your error in my end but I’m always getting a successful response. HTTP 400 status codes are usually meaning that some parameters were incorrectly introduced and/or the parameter type is not the expected. Which data type are you passing over for the image_base64 field? Is it string?

Also, for your information, ObjectCut API only supports POST methods and x-www-form-urlencoded data type for the request body.

This is a C# example generated by the RapidAPI tool that worked for me:

var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("x-rapidapi-key", "TO_BE_FILLED");
request.AddHeader("x-rapidapi-host", "background-removal.p.rapidapi.com");
request.AddParameter("application/x-www-form-urlencoded", "image_base64=%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F2wBDAA...&output_format=base64", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);```

> Note that image_base64 value is cut for a shorter answer.

Let me know if you have more questions.

Thanks,

Regards,
ObjectCut team.
Rapid account: Dylan Ccq 3 Vz Krs
dylan-ccq3vzKrs Commented 3 years ago

I assume you can only pass a url of an online image and not post the image? That would really be a constraint 😦

Participez à la discussion - ajoutez un commentaire ci-dessous:

Connectez-vous / Inscrivez-vous pour publier de nouveaux commentaires