I am trying to get a JSON response using the requests module. Was wondering if anyone knows what could be causing this.
import requests
url = "https://www.google.com/"
data = requests.get(url)
data.json()
Error:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
From the docs:
In case the JSON decoding fails, r.json() raises an exception. For
example, if the response gets a 204 (No Content), or if the response
contains invalid JSON, attempting r.json() raises ValueError: No JSON
object could be decoded.
You need to have a url that could possibly return a json:
import requests
url = 'https://github.com/timeline.json'
data = requests.get(url).json()
print(data)
OUTPUT:
{'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.', 'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events'}
The page you are returning is not json, its html
Related
I am new here so pardon me for mistakes or silly questions
I am trying to implement following via python
https://docs.paloaltonetworks.com/cortex/cortex-xdr/cortex-xdr-api/cortex-xdr-apis/xql-apis/get-xql-query-exported-data.html
Intsead of data, I am getting <Response 200>
How can I get the GZIP file via this API call. This is the first time I am trying anything like this and any help would be appreciated.
My code:
def get_stream_data():
temp_headers = {
"x-xdr-auth-id": str(4),
"Authorization": config['CORTEX_KEY'],
"Accept-Encoding": "gzip",
"Content-Type":"application/json",
}
stream_id = get_XQL_query()
data_temp = {"request_data":{"stream_id":stream_id,"is_gzip_compressed": True}}
response = requests.post(url="<fqdn>.paloaltonetworks.com/public_api/v1/xql/get_query_results_stream/",headers=temp_headers,json=data_temp)
#something here to parse the response, no idea what
return response
Requests library documentation says that you need:
return response.json() # Returns JSON, if properly encoded else raises error
# or
return response.content # Return raw bytes
I've been trying to get JSON response out of the following code, but it's throwing an error
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
for item in response:
print(item, response[item])
I wanna print the JSON in the following format:
https://i.stack.imgur.com/ZWMbr.png
your code works well to get the correct respone from the url. The problem is that you should parse the raw respone as the json format. Please refer to the following code:
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
# dump response into json
result = response.content.decode('utf-8')
result = json.loads(result)
print(result) # print raw json
# print as key-value pairs
for item in result:
print("{0} : {1}".format(item, result[item]))
The output is like following:
Example output
I think you're misunderstanding what the response contains.
Your initial code is good.
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
You now have a response. see here for more info.
In this response you have status_code, content, encoding, and the response content as text. it also contains other information about the request. eg we don't want to continue if the request failed
You can simply parse the json by calling json.loads on the decoded response via response.text
parsed_json = json.loads(response.text)
or
parsed_json = response.json()
for item in json_:
print(item, json_[item])
I hope it helps. it is also duplicate to HTTP requests and JSON parsing in Python
When I use the requests library with django and I get a 500 error back. response.json() gives me this error:
response = requests.post(....)
print("-----------------------block found!!!-----------------------")
print(response.status_code)
print(response.json())
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
Is there a way to represent a django 500 response with the requests library in a readable manner?
Assuming you get an HTTP 500 response, You would definitely receive an empty source, Which prevents the .json() function from taking place.
Why not write an exception clause to handle the exceptions, like below:
try:
response = requests.post(....)
print("-----------------------block found!!!-----------------------")
print(response.status_code)
print(response.json())
except HTTPError as e:
print('Error has occurred: ', e.response.status_code)
I've been stuck trying to pull a certain bit from this api response for a while.
my code:
payload = {
'symbol':'RPX-ETH',
'from':'100603756',
'to':'9516619507'
}
request = requests.get('https://api.kucoin.com/v1/open/chart/history',
params=payload)
jdata = json.loads(request)
print jdata['c']
However I keep getting this error:
TypeError: expected string or buffer
The api response only using .json() for reference:
{u'c': [0.00024, 0.000171, 0.000163, 0.000151, 0.000159, 0.000164}
request is the whole requests response object. You need to pass request.body.
However there is no need to do that at all because request.json() does it for you and returns a parsed Python data structure.
You can use the request.json to access the return data as a dictionary.
Replace
jdata = json.loads(request)
print jdata['c']
With
jdata = request.json()
print jdata['c']
I'm using the following code to send my video and apparently I got no error. But the response is coming blank. How can I read the response?
########### Python 2.7 #############
import httplib, urllib, base64, json
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': 'xxxxxxxxxxxxxxxxxxxx',
'Content-Type': 'application/json'
}
video_filename = {"url":"https://fsarquivoeastus.blob.core.windows.net/public0/WhatsApp-Video-20160727.mp4"}
params = urllib.urlencode({})
try:
conn = httplib.HTTPSConnection('api.projectoxford.ai')
conn.request("POST", "/emotion/v1.0/recognizeinvideo?%s" % params, json.dumps(video_filename), headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
The Cognitive Service Video APIs, including Emotion, operate asynchronously, and by design return an empty response body when the POST succeeds. What you must do instead is retrieve the operation URL from the headers, as shown here:
response = conn.getresponse()
location = response.getheader('operation-location');
print(location);
You call GET on that location URL to check on the status of the operation. More on that here.
#FelipeSouzaLima, To getting the operation result from Emotion Recognition in Video, you need to do two steps as below.
Call the REST API for Emotion Recognition in Video, then you will get the blank response body and the operation-location header which will be called at the next step as #cthrash said.
Call the url of the value of operation-location header above with the same Ocp-Apim-Subscription-Key as request header, then you can get the json response body which includes the recognition job status. If the value of status field in the json response is Succeeded, you will get the operation result as processingResult field in the json result, please refer to the REST API of Get Recognition in Video Operation Result.