Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65829286/sending-an-api-request-in-flutter-image-upload

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...