import requests
I am trying to send data to an API which works fine but all of a sudden i start getting JSON error
This is the code
def payment(phone, receiver_phone, amount):
req_header = os.environ.get('APP_KEY')
payload = {
'receiver_phone': receiver_phone,
'amount': amount,
'payer_phone': phone
}
res = requests.post('https://sspay.com/payment?key={0}'.format(req_header), data=payload)
return res.json()
print(payment('07XXXXXX', '0XXXXXXXXX', '1'))
This is the output i get
Traceback (most recent call last):
File "test.py", line 25, in <module>
print(payment('07xxxxx', '09xxxxxxxx', '1'))
File "test.py", line 14, in payment
return res.json()
File "/home/pc/.virtualenvs/talk/lib/python3.8/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/home/pc/.virtualenvs/talk/lib/python3.8/site-packages/simplejson/__init__.py", line 516, in loads
return _default_decoder.decode(s)
File "/home/pc/.virtualenvs/talk/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/home/pc/.virtualenvs/talk/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
use the code below - it will make you to find the issue
req_header = os.environ.get('APP_KEY')
res = requests.post('https://sspay.com/payment?key={0}'.format(req_header), data=payload)
if res.status_code == 200:
data = res.json()
return data
else:
print('We have a problem. status code : {}'.format(res.status_code))
you are not checking the API response (status code)
what if req_header is None
Related
I have tested around 1000 bitcoin addresses and there were no problems, however after trying to test around 40,000 addresses it gives me this error
from requests import get
import pandas as pd
import json
def make_api_url():
data = pd.read_csv('bitcoinaddr.csv')
Wallet_Address = (data.loc[:,"Address"])
BASE_URL = "https://blockchain.info/balance"
for addresses in Wallet_Address:
addresses = '|'.join(Wallet_Address)
url = BASE_URL + f"?active={addresses}" #error
return url
get_balance_url = make_api_url()
response = get(get_balance_url)
j_data = response.json()
finalbalance=[]
tx=[]
totalreceived=[]
new_dataset = pd.DataFrame.from_dict(j_data, orient='index')
print(new_dataset)
finalbalance=new_dataset['final_balance'].to_list()
tx=new_dataset['n_tx'].to_list()
totalreceived=new_dataset['total_received'].to_list()
data['Final Balance']=finalbalance
data['n_tx']=tx
data['Total Received']=totalreceived
new_dataset.to_csv('CheckedBTCAddress.csv',index=True,header=True)
# https://blockchain.info/balance?active=$address
The error code shows
Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py",
line 971, in json
return complexjson.loads(self.text, **kwargs) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\json_init_.py",
line 346, in loads
return _default_decoder.decode(s) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\json\decoder.py",
line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\Forensic\AppData\Local\Programs\Python\Python310\lib\json\decoder.py",
line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"c:\Users
Scripting\BTCAddressBulkChecker.py", line 21, in
data = response.json() File "C:\UsersPython\Python310\lib\site-packages\requests\models.py",
line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1
(char 0)
here is the full error message for when i run the code that is under this error message:
PS C:\Users\Admin> & C:/Users/Admin/AppData/Local/Programs/Python/Python39/python.exe c:/Users/Admin/Desktop/dadjoke.py
Traceback (most recent call last):
File "c:\Users\Admin\Desktop\dadjoke.py", line 9, in <module>
data = json.loads(str(question.text ))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
PS C:\Users\Admin>
here is my code:
import requests
import json
params = {"q":"joke"}
url = "https://icanhazdadjoke.com"
question = requests.get(url, params)
if question.status_code == 200:
data = json.loads(str(question.text ))
print(data)
I've just take a look at the API docs of the website.
Since you didn't specify your Accept header, the website will return a HTML response as default
Solution:
headers = {
"Accept": "application/json"
}
url = "https://icanhazdadjoke.com"
r = requests.get(url, headers = headers)
I'm new to python and APIs and I'm using this tutorial to learn with last-fm. I'm getting these errors:
Traceback (most recent call last):
File "/Users/vikram03/Desktop/Python_learning/fm.py", line 89, in <module>
total_pages = int(response.json()['artists']['#attr']['totalPages'])
File "/Applications/anaconda3/lib/python3.8/site-packages/requests/models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "/Applications/anaconda3/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Applications/anaconda3/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Applications/anaconda3/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Here's the code in question:
#initializes reponses list
responses = []
page = 1
total_pages = 70
while page <= total_pages:
payload = {
'method': 'chart.gettopartists',
'limit': 500,
'page': page
}
#print
print("Requesting page {}/{}".format(page, total_pages))
#clear clear_output
clear_output(wait=True)
#make the API call
response = lastfm_get(payload)
#error check
if response.status_code != 200:
print(reponse.text)
break
#extract pagination info
page = int(response.json()['artists']['#attr']['page'])
total_pages = int(response.json()['artists']['#attr']['totalPages'])
# append
responses.append(response)
#if its not cached, sleep
if not getattr(response, 'from_cache', False):
time.sleep(0.25)
#increment
page += 1
This function: lastfm_get just does requests.get with the appropriate params and this url 'http://ws.audioscrobbler.com/2.0/'
From other posts I get the impression the program is getting non-JSON objects, but when I print
total_pages = int(response.json()['artists']['#attr']['totalPages']) I get the correct number.
edit:
this is the function:M
def lastfm_get(payload):
#define headers and url
headers = {'user-agent': USER_AGENT}
url = 'http://ws.audioscrobbler.com/2.0/'
#add API key and format
payload['api_key'] = API_KEY
payload['format'] = 'json'
response = requests.get(url, headers = headers, params = payload)
return response
edit2:
I added this code to check if I was getting a JSON object, and am getting a type error after about 200 pages:
#check object
try:
json_test = json.loads(lastfm_get(payload))
except TypeError:
print("not a json object")
Error:
Requesting page 256/7743
Requesting page 259/7743
Requesting page 275/7743
Requesting page 294/7743
Traceback (most recent call last):
File "/Users/vikram03/Desktop/Python_learning/fm.py", line 94, in <module>
page = int(response.json()['artists']['#attr']['page'])
File "/Applications/anaconda3/lib/python3.8/site-packages/requests/models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "/Applications/anaconda3/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Applications/anaconda3/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Applications/anaconda3/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I got a type error when I tried with except ValueError:
Traceback (most recent call last):
File "/Users/vikram03/Desktop/Python_learning/fm.py", line 84, in <module>
json_test = json.loads(lastfm_get(payload))
File "/Applications/anaconda3/lib/python3.8/json/__init__.py", line 341, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not Response
EDIT3: I get this error when testing the output of lastfm_get:
TypeError: Object of type Response is not JSON serializable
Unsure how to resolve this.
I'm not sure why the page numbers are skipping either
I'm using python requests module, and the two ways result in different results.
Giving params into url:
url = 'http://my_url?m=getMember&mail=abc#de.com'
requests.get(url=url).json()
Giving params separately:
url = 'http://my_url'
data = {'m': 'getMember', 'mail':'abc#de.com'}
requests.get(url = url, data = data).json() #also tried 'data = json.dumps(data)'
The first way gives the right result.
But the second way occurs the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home1/irteam/.local/lib/python3.6/site-packages/requests/models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
And both have the response codes of 200.
>>> r = requests.post(url = url, data = json.dumps(data))
>>> r
<Response [200]>
>>> r = requests.post(url = url, data = data)
>>> r
<Response [200]>
When I type r.text,
>>> r.text
''
What could be the difference between the two?
Thanks:)
By passing data argument you are sending a payload with get request. But not setting the params.
For setting params there is another argument. So this would work:
requests.get(url = url, params = data).json()
Hope this helps.
I am trying to call API from postman but I am getting an error in my console.
In the below code, I am trying to decode it and process further according to it.
API- https://localhost:5005/abc/xyz
Method- POST
Data - {"q":"hi"}
server.py
def request_parameters(request):
if request.method.decode('utf-8', 'strict') == 'GET':
return {
key.decode('utf-8', 'strict'): value[0].decode('utf-8',
'strict')
for key, value in request.args.items()}
else:
content = request.content.read()
try:
return json.loads(content.decode('utf-8', 'strict'))
except ValueError as e:
logger.error("Failed to decode json during respond request. "
"Error: {}. Request content: "
"'{}'".format(e, content))
raise
Full stacktrace
Failed to decode json during respond request. Error: Expecting value: line 1 column 1 (char 0). Request content: 'b'''
2019-05-14 18:21:53+0530 [-] Unhandled Error
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\twisted\web\server.py", line 258, in render
body = resrc.render(self)
File "C:\Anaconda3\lib\site-packages\klein\resource.py", line 210, in render
d = defer.maybeDeferred(_execute)
File "C:\Anaconda3\lib\site-packages\twisted\internet\defer.py", line 151, in maybeDeferred
result = f(*args, **kw)
File "C:\Anaconda3\lib\site-packages\klein\resource.py", line 204, in _execute
**kwargs)
--- <exception caught here> ---
File "C:\Anaconda3\lib\site-packages\twisted\internet\defer.py", line 151, in maybeDeferred
result = f(*args, **kw)
File "C:\Anaconda3\lib\site-packages\klein\app.py", line 128, in execute_endpoint
return endpoint_f(self._instance, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\klein\app.py", line 227, in _f
return _call(instance, f, request, *a, **kw)
File "C:\Anaconda3\lib\site-packages\klein\app.py", line 50, in _call
result = f(*args, **kwargs)
File "server.py", line 61, in parse
request_params = request_parameters(request)
File "server.py", line 22, in request_parameters
return json.loads(content.decode('utf-8', 'strict'))
File "C:\Anaconda3\lib\site-packages\flask\json\__init__.py", line 205, in loads
return _json.loads(s, **kwargs)
File "C:\Anaconda3\lib\site-packages\simplejson\__init__.py", line 535, in loads
return cls(encoding=encoding, **kw).decode(s)
File "C:\Anaconda3\lib\site-packages\simplejson\decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "C:\Anaconda3\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
script.js
function respond(msg) {
data = {
query: msg //msg is getting from chatbot
}
fetch(`${url}/conversations/default/respond`, {
mode: 'no-cors',
method: 'POST',
// dataType:'jsonp',
q: data,
headers: {
'Content-Type': 'application/json',
},
})
You can get the content directly in json with request.get_json()