How to upload an image to the server with chopper library? I tried the search on google but, I couldn't manage to get one.
What I have tried is
Function handle creating the chopper client
static ApiService create(String accessToken) {
final client = ChopperClient(
baseUrl: 'http://192.168.43.125:8000/api',
services: [
_$ApiService(),
],
converter: JsonConverter(),
interceptors: [
HeadersInterceptor({
"Content-Type": "application/x-www-form-urlencoded",
'Authorization': 'Bearer $accessToken',
'Accept': 'application/json',
}),
HttpLoggingInterceptor()
]);
return _$ApiService(client);}
API
@Post(path: '/inspectionStore',)
@multipart
Future<Response> storeInspection(
@Part("field") String field, @PartFile("file") http.MultipartFile file);
Code that do the work
File actualFile = photoSection.postImage[0];
http.MultipartFile file = await http.MultipartFile.fromPath('file', actualFile.path,contentType: MediaType('image', 'jpg'));
var response = await Provider.of<ApiService>(context).storeInspection('some name',file);
This is what the server retrieve (Laravel Log file)

How can I get a proper data that can be used?
I know it's a little late but this could save someone. So, you need to provide the path to your image as string
@multipart
Future<Response> storeInspection(
@Part("field") String field, @PartFile("file") String file);
then
File actualFile = photoSection.postImage[0];
var response = await Provider.of<ApiService>(context).storeInspection('some name',file.path);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With