Skip to content

Commit

Permalink
refactor: Pass Accept header to requests in contrib.utils.download (
Browse files Browse the repository at this point in the history
#1673)

* Pass requests the `Accept` HTTP header that matches the content type
of the resource file. The HEPData landing page for the resource
requested can check if the header matches and then return the content
directly.
   - c.f. https://blog.datacite.org/changes-to-doi-content-negotiation/

Co-authored-by: Graeme Watt <Graeme.Watt@durham.ac.uk>
  • Loading branch information
matthewfeickert and GraemeWatt authored Nov 1, 2021
1 parent fbdce47 commit 377b85d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/pyhf/contrib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ def download(archive_url, output_directory, force=False, compress=False):
+ "To download an archive from this host use the --force option."
)

with requests.get(archive_url) as response:
# c.f. https://github.com/scikit-hep/pyhf/issues/1491
# > Use content negotiation at the landing page for the resource that
# > the DOI resolves to. DataCite content negotiation is forwarding all
# > requests with unknown content types to the URL registered in the
# > handle system.
# c.f. https://blog.datacite.org/changes-to-doi-content-negotiation/
# The HEPData landing page for the resource file can check if the Accept
# request HTTP header matches the content type of the resource file and
# return the content directly if so.
with requests.get(
archive_url, headers={"Accept": "application/x-tar"}
) as response:
if compress:
with open(output_directory, "wb") as archive:
archive.write(response.content)
Expand Down

0 comments on commit 377b85d

Please sign in to comment.