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

Feature request: download files #155

Open
adamblake opened this issue Nov 22, 2024 · 1 comment
Open

Feature request: download files #155

adamblake opened this issue Nov 22, 2024 · 1 comment

Comments

@adamblake
Copy link

I have an API that returns files for some of the endpoints. I like to use Posting when I am messing around with it and trying out new ways to do things. I didn't see an easy way of writing the response to file, so I have been using a Post-response script that looks like this:

def on_response(response, posting) -> None:
    content_disposition = response.headers.get("Content-Disposition")
    if content_disposition and "attachment" in content_disposition:
        import re
        from pathlib import Path

        # Extract filename from Content-Disposition
        match = re.search(r'filename="?(?P<filename>[^"]+)"?', content_disposition)
        filename = match.group("filename") if match else "downloaded_file"

        # Ensure filename is safe enough
        filename = Path(filename).name

        # Set download directory
        download_dir = Path.cwd() / ".downloads"
        download_dir.mkdir(exist_ok=True)

        # Write content to file
        file_path = download_dir / filename
        file_path.write_bytes(response.content)

It's obviously not robust enough for a production-ready feature, but it works for now. I would prefer if the app had a feature that did something like this that I could turn on in the options, or something like that.

@adamblake
Copy link
Author

By the way, your app is awesome

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

1 participant