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);
Author by
Ace
Updated on December 27, 2022Comments
-
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 about 3 yearsTry use some of solutions for similar question.
-
Kennedypz about 3 yearsCan you show us the errors you're getting?
-
Ace about 3 years@fartem Thank you so much. One of the solutions worked for me.
-