infer_extension#

scikitplot.corpus.infer_extension(headers, url)[source]#

Infer a file extension from HTTP response headers and URL path.

Public wrapper around _infer_extension_from_headers. Call this when you already hold response headers (e.g. after a manual requests.head()) and want to know what extension to use for the downloaded file.

The resolution order is:

  1. URL path extension (cheapest, most reliable when present).

  2. Content-Disposition filename*= (RFC 5987 encoded form).

  3. Content-Disposition filename= (plain form).

  4. Content-Type MIME mapping (skips application/octet-stream).

  5. mimetypes.guess_extension stdlib fallback.

  6. ".bin" when nothing can be inferred.

Parameters:
headersdict-like or http.client.HTTPMessage

HTTP response headers that support .get(key, default).

urlstr

Original request URL. Used for path-based extension lookup and as a logging label.

Returns:
str

File extension including leading dot, e.g. ".pdf". Returns ".bin" if nothing can be inferred.

Parameters:
Return type:

str

Examples

>>> infer_extension({"Content-Type": "audio/mpeg"}, "https://host/dl")
'.mp3'
>>> infer_extension({}, "https://host/report.pdf")
'.pdf'
>>> infer_extension(
...     {"Content-Disposition": "attachment; filename*=UTF-8''report%20final.pdf"},
...     "https://host/dl",
... )
'.pdf'