Content Disposition Filename Parse
Parse Content-Disposition headers for safe filename extraction with RFC 5987 support.
Code
from urllib.parse import unquote
import re
def parse_filename(header):
if not header:
return None
m = re.search(r"filename\*=([^;]+)", header)
if m:
parts = m.group(1).split("'")
if len(parts) == 3:
return unquote(parts[2])
return unquote(parts[-1])
m = re.search(r'filename="?([^";]+)"?', header)
if m:
return m.group(1)
return NoneHow it works
Content Disposition Filename Parse comes down to the function parse_filename.
From urllib it pulls unquote — the imports name exactly which library surface the snippet touches.
What to watch for
- Plan for an install step:
urllibis a real dependency, resolved by the package manager.
Putting it to use
Entry signature: def parse_filename(header)
parse_filename is what another module would consume; nothing else here is load-bearing for callers.
Interactive Tools for This Topic
Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.Cache-Control BuilderCompose a valid Cache-Control header from directives such as no-store, private, max-age, and immutable.
FAQ
- Are there third-party dependencies?
- `urllib` — install it with the package manager before running.
Report a problem with this exampleOpens a pre-filled issue on GitHub.