so I am working on a flutter project where I am using mongodb as my database, NextJS as the backend and flutter for the frontend.
This project is a team work between me and my colleague. I am responsible for the front and he for the back.
He provided me with an API Url that takes the following fields:
-‘files’ : a file
-‘categorie’ : a string
-‘userId’ : a string
When I tested the API through postman, the file got uploaded successfully.
Here is screenshot of the test using the same exact token as bearer authorization:
The postman test
The problem happens however when i try to use the api code. now matter how hard i tried and researched, i couldn’t find a solution from my end to the 401 problem.
here is the code
Future<void> sendFileToDatabase(FileItem file, File pdfAsFile) async {
try {
final url = Uri.parse('http://192.168.1.17:3000/api/file/UploadFile');
final request = http.MultipartRequest('POST', url);
String accessTok = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIyMDI0Iiwic3ViIjoiNjY0OGM0MzgwZjFlMTU4ZGRkY2RjYjEwIiwicm9sZXMiOlsiY29tcGFueSJdLCJpYXQiOjE3MTYzOTczODYsImV4cCI6MTcxNjQxNTM4Nn0.tV8eJs_Tv69UM10akte7vlOrMsvVYjROyu9b2zCY3BM";
// Add the authentication header
Map<String, String> headers = { "Authorization":"Bearer "+ accessTok};
request.headers.addAll(headers);
Uint8List bytes = await pdfAsFile.readAsBytes();
print("the bytes : $bytes n request Auth : ${request.headers['Authorization']}");
// Add the PDF file
final fileField = http.MultipartFile.fromBytes('files', bytes, filename: file.filename);
request.files.add(fileField);
// Add the other fields
request.fields['category'] = 'dossier juridique';
request.fields['user'] = file.userID;
final response = await request.send();
if (response.statusCode == 200) {
print('File uploaded successfully');
} else {
print("response : ${response.reasonPhrase}");
print('Failed to upload file: ${response.statusCode}');
}
}
catch(e){
print("error du sending $e");
}
}
and this is the error message along with the print of the bytes :
the bytes : [37, 80, 68, 70, 45, 49, 46, 52, 10, 37, 211, 235, 233, 225, 10, 49, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 84, 105, 116, 108, 101, 32, 60, 70, 69, 70, 70, 48, 48, 53, 52, 48, 48, 52, 52, 48, 48, 50, 48, 48, 48, 54, 69, 48, 48, 66, 48, 48, 48, 51, 51, 48, 48, 53, 70, 48, 48, 52, 67, 48, 48, 54, 49, 48, 48, 50, 48, 48, 48, 53, 50, 48, 48, 54, 49, 48, 48, 54, 52, 48, 48, 54, 57, 48, 48, 54, 70, 48, 48, 54, 55, 48, 48, 55, 50, 48, 48, 54, 49, 48, 48, 55, 48, 48, 48, 54, 56, 48, 48, 54, 57, 48, 48, 54, 53, 48, 48, 50, 48, 62, 10, 47, 80, 114, 111, 100, 117, 99, 101, 114, 32, 40, 83, 107, 105, 97, 47, 80, 68, 70, 32, 109, 49, 50, 54, 32, 71, 111, 111, 103, 108, 101, 32, 68, 111, 99, 115, 32, 82, 101, 110, 100, 101, 114, 101, 114, 41, 62, 62, 10, 101, 110, 100, 111, 98, 106, 10, 51, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 99, 97, 32, 49, 10, 47, 66, 77, 32, 47, 78, 111, 114, 109, 97, 108, 62, 62, 10, 101, 110, 100, 111, 98, 106, 10, 54, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 70, 105, 108, 116,
I/flutter ( 4306): response : Unauthorized
I/flutter ( 4306): Failed to upload file: 401
My question is, what is the error that I have done with my code if it does exist? And if not, is it a problem from the backend side?
Thank you to anyone who takes the time to reply 🙂
Saif Eddine Romdhane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.