Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom HTTP Headers in Network Requests #132

Closed
akshatshah opened this issue Apr 18, 2024 · 6 comments
Closed

Custom HTTP Headers in Network Requests #132

akshatshah opened this issue Apr 18, 2024 · 6 comments

Comments

@akshatshah
Copy link

We have been trying to figure out a way to use the PdfViewer.uri widget with a link that requires authenticated headers. The current setup we have uses a separate cache mechanism to download the file and then pass it on to the PdfViewer widget. Being able to pass the headers directly to the widget would be optimal in terms of caching and performance. Any insight on this would be appreciated. I would also be willing to work on a PR for this if you're open to it!

@espresso3389
Copy link
Owner

espresso3389 commented Apr 19, 2024

Could you please give me more info about your pains or requests?
I'm always open to any such PR but I want to know more about your situation.

@akshatshah
Copy link
Author

Essentially let's say you have a pdf link that needs a auth token to enable access for the client device. I wasn't able to find a way to pass headers to the http request.

Current Setup:

Get the file from cache (download if not available already):

  Future<Either<ApiFailure, String>> getFilePath(String url) async {
    try {
      final credentials = await _authController.getUserCredentials();
      final headers = credentials.authHeader;
      final file = await _cacheManager.getSingleFile(
        url,
        headers: headers,
      );
      return right(file.path);
    } catch (e) {
      return left(ApiFailure.platform(LocaleKeys.standardErrorMessage.tr()));
    }
  }

Pass the path to the cached file in the PdfViewer

 return PdfViewer.file(
      path,

Ideal Setup would be to pass the headers to the widget itself (or having a custom client option that can make authenticated requests). Here the headers would have a bearer token that allows the client to access the file:

PdfViewer.uri(
 uri,
 headers: {
   'Authorization': 'Bearer <token>'
....

Maybe I am missing something and this feature already exists. I would love some guidance if that's the case.

@espresso3389
Copy link
Owner

Could you please check 5717726?

It introduces headers to PdfDocument.openUri function.

Map<String, String>? headers,

@espresso3389
Copy link
Owner

1.0.57 contains the fix above.

@akshatshah
Copy link
Author

Hey, sorry for the delayed response. I just tried to test but there is no way to pass the headers to the PdfDocument.openUri function from the widget. I went through the code and adding links to places where headers parameter would be needed below so that we can pass it from the widget. Thank you for all your help so far!

PdfViewer.uri(

class PdfDocumentRefUri extends PdfDocumentRef

Future<PdfDocument> loadDocument(

preferRangeAccess: preferRangeAccess,

espresso3389 added a commit that referenced this issue May 4, 2024
@akshatshah
Copy link
Author

@espresso3389 I tested the latest release and it works. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants