Skip to content

Commit

Permalink
Added a way to supply the mime type to upload_datafile(); and made th…
Browse files Browse the repository at this point in the history
…e method use the default content type when encoding the POST request if it's not supplied explicitly. (gdcc#118)
  • Loading branch information
landreev committed Jan 21, 2022
1 parent cc06022 commit 628dbb4
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/pyDataverse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ def get_datafile_metadata(
# CHECK: Its not really clear, if the version query can also be done via ID.
return self.get_request(url, auth=auth)

def upload_datafile(self, identifier, filename, json_str=None, is_pid=True):
def upload_datafile(self, identifier, filename, json_str=None, is_pid=True, content_type='application/octet-stream'):
"""Add file to a dataset.
Add a file to an existing Dataset. Description and tags are optional:
Expand All @@ -1669,6 +1669,8 @@ def upload_datafile(self, identifier, filename, json_str=None, is_pid=True):
Metadata as JSON string.
is_pid : bool
``True`` to use persistent identifier. ``False``, if not.
content_type : str
MIME type. Defaults to ``application/octet-stream``; this will prompt Dataverse to attempt to identify the MIME type of the file more accurately.
Returns
-------
Expand All @@ -1683,7 +1685,7 @@ def upload_datafile(self, identifier, filename, json_str=None, is_pid=True):
else:
url += "/datasets/{0}/add".format(identifier)

files = {"file": open(filename, "rb")}
files = {"file": (filename, open(filename, "rb"), content_type)}
return self.post_request(
url, data={"jsonData": json_str}, files=files, auth=True
)
Expand Down

0 comments on commit 628dbb4

Please sign in to comment.