I wrote a certain API wrapper using Python's requests library.
When it gets a response using requests.get, it attempts to parse as json and takes the raw content if it doesn't work:
resp = requests.get(url, ...)
try:
resp_content = resp.json()
except ValueError:
resp_content = resp.content
return resp_content
This is correct for my purposes. The problem is how long it takes when the downloaded response is an image file, for example, if it is large, then it takes an extremely long time between entering the try, and failing the json parse and entering the except.
(I don't know if it takes super long for the .json() to error at all, or if once it errors it then takes a while to get into the except.)
Is there a way to see if resp is json-parsable without attempting to parse it with .json()? Something like resp.is_json, so I can instantly know which branch to take (resp.json() or resp.content), instead of waiting 30 seconds (large files can take minutes).
EDIT:
As noted, this slowness is not typical for requests json parsing. It may have to do with the nature of the data I am receiving (it is from Salesforce REST API, retrieving Attachment object's 'Body' field).
Even though it's a workaround, I will put my solution here in case the strategy helps anyone else. I realized where I'm making the call, I usually know if I expect the response to be binary data, so I can pass a keyword argument to my wrapper function that tells it to skip the json parsing attempt.
def SalesforceWrapper(..., attempt_json=True):
resp = requests.get(url, ...)
try:
if attempt_json:
resp_content = resp.json()
else:
resp_content = resp.content
except ValueError:
resp_content = resp.content
return resp_content
and then I pass attempt_json=False when I expect the response to be a file's data, not a lil JSON response.