Ace
Ace

Reputation: 132

Sending an API request in Flutter. Image Upload

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?

Upvotes: 2

Views: 258

Answers (1)

Ace
Ace

Reputation: 132

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

Upvotes: 3

Related Questions