Skip to content

Commit 8b13ec8

Browse files
Add JSON validation to _guess_doi and make Content-Type check case-insensitive
Co-authored-by: marc-hanheide <1153084+marc-hanheide@users.noreply.github.com>
1 parent 861f38a commit 8b13ec8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

figshare.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def shorten(self, doi):
5555
try:
5656
response = requests.get(url)
5757
# Check if response is valid and contains JSON
58-
if response.ok and response.headers.get('Content-Type', '').startswith('application/json') and response.text.strip():
58+
if response.ok and response.headers.get('Content-Type', '').lower().startswith('application/json') and response.text.strip():
5959
result = response.json()
6060
short_doi = result['ShortDOI']
6161
else:
@@ -158,7 +158,7 @@ def __get(self, url, params=None, use_cache=True):
158158
headers = { "Authorization": "token " + self.token } if self.token else {}
159159
response = get(self.base_url + url, headers=headers, params=params)
160160
# Check if response is valid and contains JSON
161-
if response.ok and response.headers.get('Content-Type', '').startswith('application/json') and response.text.strip():
161+
if response.ok and response.headers.get('Content-Type', '').lower().startswith('application/json') and response.text.strip():
162162
result = response.json()
163163
self.__cache[hash_key] = result
164164
self.save_cache()
@@ -175,7 +175,7 @@ def __post(self, url, params=None, use_cache=True):
175175
headers = { "Authorization": "token " + self.token } if self.token else {}
176176
response = post(self.base_url + url, headers=headers, json=params)
177177
# Check if response is valid and contains JSON
178-
if response.ok and response.headers.get('Content-Type', '').startswith('application/json') and response.text.strip():
178+
if response.ok and response.headers.get('Content-Type', '').lower().startswith('application/json') and response.text.strip():
179179
result = response.json()
180180
self.__cache[hash_key] = result
181181
self.save_cache()
@@ -294,7 +294,13 @@ def _guess_doi(self, article):
294294
self.logger.debug(f"Querying Crossref for title: {title}")
295295
response = requests.get(base_url, params=params)
296296
response.raise_for_status()
297-
data = response.json()
297+
298+
# Check if response is valid and contains JSON
299+
if response.ok and response.headers.get('Content-Type', '').lower().startswith('application/json') and response.text.strip():
300+
data = response.json()
301+
else:
302+
self.logger.warning(f"Received empty or invalid JSON response from Crossref API (status: {response.status_code})")
303+
return None
298304

299305
if data["message"]["total-results"] == 0:
300306
self.logger.debug(f"No DOI found for: {title}")

0 commit comments

Comments
 (0)