Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests - quickly know if response is json parsable

Tags:

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.

like image 490
tscizzle Avatar asked May 16 '17 22:05

tscizzle


People also ask

How check response is JSON or not in Python?

json() – Python requests. response. json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error).

How do I know if a response is JSON?

JSON Check The fetch . then() callback is passed the HTTP response object when the request is completed, the function checks if the response type is JSON before parsing the response body with the response. json() method, because calling response. json() will cause an error if the response doesn't contain JSON data.

How do you assert a JSON response in Python?

@pytest. mark. parametrize("fixture_name", [(path, json)], indirect=True) def test_the_response_status_code_first(fixture_name): assert fixture_name. ok, "The message for the case if the status code !=

How do I check if a response JSON is empty in Python?

If you want to check if your response is not empty try : if ( json. length == 0 ) { console. log("NO DATA!") }


1 Answers

Depending on the consistency of the response, you could check if the returned headers include content-type application/json:

resp.headers.get('content-type') == 'application/json'

like image 73
dizzyf Avatar answered Sep 16 '22 14:09

dizzyf