Sending an API request in Flutter. Image Upload

127

i found a solution that worked for me. Here's the correct way of Sending an Authenticated API Request with a file.

var request = http.MultipartRequest("POST", Uri.parse("<URL>"));
//add Headers
request.headers['Api-Key'] = 'Your API key';
//create multipart using filepath, string or bytes
var pic = await http.MultipartFile.fromPath("image", file.path);

//add multipart to request
request.files.add(pic);
var response = await request.send();
var responseData = await response.stream.toBytes();
var responseString = String.fromCharCodes(responseData);
print(responseString);
Share:
127
Ace
Author by

Ace

Updated on December 27, 2022

Comments

  • Ace
    Ace over 1 year

    I'm trying to send an API request to the DeepApi Toonify website using flutter but I'm having some issues as I do not understand how to go about doing it.

    This is how the toonify API request should look like

    curl \
    -F 'image=YOUR_IMAGE_URL' \
    -H 'api-key:quickstart-QUdJIGlzIGNvbWluZy4uLi4K' \
    https://api.deepai.org/api/toonify 
    

    This is what I have right now.

    var dioRequest = new Dio();
    
    FormData formData = FormData.fromMap({
      "file": await MultipartFile.fromFile(pickedFile.path,filename: pickedFile.path.split('/').last)
    });
    
    
    var response = await dioRequest.post(
      'https://api.deepai.org/api/toonify',
      data: formData,
      options: Options(
        headers: {
          'api-key': 'MY API KEY'
        }
      )
    );
    

    What am I doing wrong?

    • fartem
      fartem about 3 years
      Try use some of solutions for similar question.
    • Kennedypz
      Kennedypz about 3 years
      Can you show us the errors you're getting?
    • Ace
      Ace about 3 years
      @fartem Thank you so much. One of the solutions worked for me.