Home > Blockchain >  Flutter: Http Status Error 403 while downloading the file
Flutter: Http Status Error 403 while downloading the file

Time:09-14

In my Flutter application, I have to download multiple files and have to store it in application storage.

For that I am using Dio and added below plugin dependencies in pubspec.yaml file :

dio: any
path_provider: any

In main.dart, I have below variables :

String uri =
      'https://i.picsum.photos/id/698/200/200.jpg?hmac=EElVlYPe8BAq1Btf4bWUxP9NoQP01_e8LTUzpbdKbgY';
bool downloading = false;

where uri is the URL for an Image online.

Inside initState() method, called the downloadFile() method, which is as below :

Future<void> downloadFile() async{
Dio dio= Dio();
try{
  var dir= await getApplicationDocumentsDirectory();
  await dio.download(uri, "${dir.path}/myimg.jpg", onReceiveProgress: (rec,total){
      setState(() {
        downloading=true;
      });
  });
}catch(e)
{
  print("Error >> " e.toString());
}
setState(() {
  downloading=false;
  print("Download Completes");
  });  
 }

But, I am getting below error :

HTTP Status Error 403

MSG_WINDOW_FOCUS_CHANGED 1 1 D/InputMethodManager(15750): startInputInner - Id : 0 I/InputMethodManager(15750): startInputInner - mService.startInputOrWindowGainedFocus D/InputMethodManager(15750): startInputInner - Id : 0 I/flutter (15750): DioError [DioErrorType.response]: Http status error [403] I/flutter (15750): Source stack: I/flutter (15750): #0 DioMixin.fetch (package:dio/src/dio_mixin.dart:488:35) I/flutter (15750): #1 DioMixin.request (package:dio/src/dio_mixin.dart:483:12) I/flutter (15750): #2 DioForNative.download (package:dio/src/entry/dio_for_native.dart:84:24) I/flutter (15750): #3 _MyHomePageState.downloadFile (package:downloadfiles/main.dart:82:17) I/flutter (15750): I/flutter (15750): Download Completes

I am getting this error while running the app in android device, I have manually given File storage permission from settings for the application. Also updated AndroidManifest.xml file as below :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>

Still am getting above error. What might causing the issue?

CodePudding user response:

The HTTP 403 Forbidden response status code indicates that the server understands the request but refuses to authorize it.

This status is similar to 401, but for the 403 Forbidden status code, re-authenticating makes no difference. The access is permanently forbidden and tied to the application logic, such as insufficient rights to a resource.

String uri =
  'https://i.picsum.photos/id/698/200/200.jpg?hmac=EElVlYPe8BAq1Btf4bWUxP9NoQP01_e8LTUzpbdKbgY';

This URL Asks for authorization in headers.

So use Authorization via headers for calling API.

Like this,

Future<void> downloadFile() async {
  Dio dio = Dio();
   try {
     var dir = await getApplicationDocumentsDirectory();
     await dio.download(uri,
     options: Options(
       headers: {"Authorization": "YOUR_AUTH_KEY HERE"},
     ),
    "${dir.path}/myimg.jpg",
     onReceiveProgress: (rec, total) {
       setState(() {
         downloading = true;
       });
    });
  } catch (e) {
    print("Error >> "   e.toString());
  }
  setState(() {
    downloading = false;
    print("Download Completes");
  });
}
  • Related