How to implement an API call sending a binary file

I have to impement an API and the documentation is as follows:

Captura de pantalla 2024-05-20 a la(s) 13.35.15

I never send an image via binary file in an API call.
The various xojo codes chatgtp suggested to me I didn`t have conditions to make them work.
Someone could give me some advice about how to do it ?

Complete API reference is here: Foodvisor Vision


It wants you to upload the image encoded in a multipart/form-data upload. You can use this open source project to generate the form in Xojo:

Or use any of the 3rd party HTTP libraries like curlmbs, which include support for multipart form uploads.

1 Like

Thanks @Andrew_Lambert
I tried both ways: a) your class and b) my own way, and:

  1. have different responses
  2. I am not receiving the right response which would bethe analysis of the food that it is in the image.
    This are the two different answers:

    I will ask you if you please would check my code in order to find what I am implementing wrong in order to receive from the API the right response.
    You could find the project in the following link, including the image to be used: Dropbox Link
    THANKS !!!

Have you tried with CURL just to make sure everything works?

From their website:

# Analyze a png image
curl -X POST \
    -H "Authorization: Api-Key <your_api_key>" \
    -F "image=@path/to/image.png" \<version>/<lang>/analysis/

Maybe they have other info somewhere?

1 Like

Try adding the image data as a string instead of a file:

Dim file As FolderItem = getfolderitem("").child("food.png")
Dim multipartContent as MultipartFormDataContent = new MultipartFormDataContent()
Dim bs As BinaryStream = BinaryStream.Open(file)
Dim imgdata As String = bs.Read(bs.Length)

multipartContent.Add("image",imgdata) ' string instead of file

In btnOwn.Pressed(), you are not actually sending the formData to the server.

Replace this line:

MyURLConn.RequestHeader("Content-Type") = "multipart/form-data; boundary=" + boundary

With this:

MyURLConn.SetRequestContent(formData, "multipart/form-data; boundary=" + boundary)

@Andrew_Lambert , thank you VERY much.
People like you makes this forum an amazing one.
I am so happy to had choosen XOJO as my main programming language for my company in first place because of the people that participates in the forum. Friendly, always willing to help, responding in record time.
This also is for @AlbertoD and for many many others.
I mention only you two guys because you ansewer this particular topic, but the forum is full of people with these same qualities.