I have a problem when i want to send JSON data with a post request python API (Python 3.9). I have a list of string dictionaries that i convert to JSON using json.loads() method but it returns an error :
Traceback (most recent call last):
File "/product/tedh/environment/callminer_py3/lib/python3.9/site-packages/requests/models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "/product/tedh/environment/callminer_py3/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/product/tedh/environment/callminer_py3/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/product/tedh/environment/callminer_py3/lib/python3.9/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)
My code :
consumer_identifiers = bytes(f"{CONSUMER_USERNAME}:{CONSUMER_PASSWORD}", encoding='utf-8')
headers: requests.structures.CaseInsensitiveDict = CaseInsensitiveDict()
headers["Basic-Authorization"] = f"Basic {b64encode(consumer_identifiers).decode('ascii')}"
headers["Authorization"] = f"Bearer {self.__token['access_token']}"
metadata_body_list = self.__fill_metadata()
logger.info("Start send metadata to Callminer...")
logger.info(f"The number of metadata sent is : {len(metadata_body_list)}")
assert METADATA_INGESTION_URL is not None
for metadata_body in metadata_body_list:
json_metadata_body = json.loads(metadata_body)
try:
response = requests.post(METADATA_INGESTION_URL, headers=headers, json=json_metadata_body)
if response.json():
if "CorrelationId" in response.json() and "MiningId" in response.json() and \
response.json()["CorrelationId"] in metadata_body:
logger.debug(f"Response POST request {response.json()}")
else:
raise RuntimeError(
f"Error occurred when sending metadata to Callminer.\n Response: {response.json()}.")
except requests.exceptions.HTTPError as e:
raise RuntimeError(f"Error: {str(e)}")
logger.info("End of sending metadata.")
When i print the json, i see that it's correct:
{'Metadata': [{'Key': 'udf_text_14', 'Value': 'ACTIF'}, {'Key': 'udf_text_16', 'Value': 'F2K-140 Go 5G'}, {'Key': 'udf_text_17', 'Value': 'None'}, {'Key': 'udf_text_18', 'Value': 'None'}, {'Key': 'udf_text_23', 'Value': '2019-01-15 10:12:21'}, {'Key': 'udf_text_19', 'Value': 'NON'}, {'Key': 'udf_text_21', 'Value': 'OUI'}, {'Key': 'udf_text_20', 'Value': 'NON'}, {'Key': 'udf_text_22', 'Value': 'None'}], 'SourceId': 'SFRALLO', 'Correlationid': '78293941'}
I finally found the cause of my problem, it's the token expiration, my token expires after 1h and my request takes much longer because i have too much JSON to send, I re-generate it before the expiration time.
Related
I have a problem with API POST, can you help me?
import requests
import json
url = 'http://xxx/api/getTotalPrice'
param = dict(itineraryType=1,
departureAirportCode='HAN',
destinationAirportCode='DLI',
departureDate='2020-12-30T14:00',
returnDate='2020-12-30T09:00',
adult=1,
children=1,
infant=1
)
resp = requests.post(url=url, params=param)
u_data = resp.json()
print(u_data)
I want to receive data from API POST. This is body API and this made error.
Here, this is error
C:\Users\Admin\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/untitled/venv/test.py
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\untitled\venv\test.py", line 17, in <module>
u_data = resp.json()
File "C:\Users\Admin\PycharmProjects\untitled\venv\lib\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
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)
Process finished with exit code 1
I can get data from POST API without body, but with body, I cannot.
Can you help to fix code?
Many thanks!
P/S:
Here value which I want to get
{
"departureFlight": {
"totalPrice": 1896000.0,
"airlineCode": "VN"
},
"returnFlight": {
"totalPrice": 3263800.0,
"airlineCode": "VJ"
}
}
OK, I have an answer.
data = {'itineraryType':1,'departureAirportCode':'HAN','destinationAirportCode':'DLI','departureDate':'2020-12-30T15:00','returnDate':'2020-12-30T09:00','adult':1,'children':1,'infant':1}
headers={'Content-type':'application/json', 'Accept':'application/json'}
resp = requests.post(url=url, json=data, headers=headers)
u_data = resp.json()
print(u_data)
change dict(...) to {...} and set headers.
I have following json file:
{'a': {'$gt': datetime.datetime(2020, 1, 1, 0, 0)}}
I converted it into string and replaces ' --> "":
>>> x
'{"a": {"$gt": datetime.datetime(2020, 1, 1, 0, 0)}}'
Getting error in loading the json:
>>> json.loads(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python36\lib\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 15 (char 14)
The issue has got resolved. The issue arising from the fact : “datetime.datetime not JSON serialize”
from json import dumps
from datetime import date, datetime
def json_serial(obj):
"""JSON serializer for objects not serializable by default json code"""
if isinstance(obj, (datetime, date)):
return obj.isoformat()
raise TypeError ("Type %s not serializable" % type(obj))
After importing this function:
>>> x
{'a': {'$gt': datetime.datetime(2020, 1, 1, 0, 0)}}
>>> x=dumps(x,default=json_serial)
>>> x
'{"a": {"$gt": "2020-01-01T00:00:00"}}'
>>>
>>>
>>> json.loads(x)
{'a': {'$gt': '2020-01-01T00:00:00'}}
>>>
I'm trying to grab a response from an api but i cannot load it in a dictionary.
Here's my code:
r = session.post(url, headers=headers, json=payload)
The returned status code is 200 so i get a response.
By trying to do: reply = r.json() i get:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/honeymonitor/main.py", line 46, in <module>
data = get_data(session)
File "C:/Users/PycharmProjects/honeymonitor/main.py", line 35, in get_data
reply = r.json()
File "C:\Users\PycharmProjects\honeymonitor\venv\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\AppData\Local\Programs\Python\Python37\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)
And by using reply = r.text i get a lot of gibberish:
� d�M���)b�b��$�j�np������X2�Al�w{g��wO�n-���w��g�����}�1�Y5#�R��/�
� X���o]>6,�N�_#�#Ң����K�#���s
��Ɩ�ː�����2��N�:D��p�����7{K��������V��
-")�p0n#I�f!���Q�+| /We�.�p�=����u���-�ʌ�cs�"jfH����}qkː��q�,q�bږq�ٟ��Z���>��U#�����ET�V/��+cU����ʁ�LLe��(���V�2���å}�UC���#�8y��B�7Λ��c���Y?��q��TOMTv{����ߎ������,�[�|��wf_�˰�g������?
The content type in the headers of the post request is:
'Content-Type': 'application/json;charset=UTF-8',
Also by checking the r.encoding i get None
At the end i've tried json.loads(r.text) but i get the same error.
Also tried encoding the response with r.content.decode('utf-8')
and got UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 1: invalid continuation byte
What i'm doing wrong?
EDIT:
I've tried replicating the request with Fiddler and i can clearly see the response json there
I've compared the response headers between Fiddler and requests, the only difference is that with requests i ge 'Transfer-Encoding': 'chunked' in the headers
EDIT2:
Here's a portion of the r.content:
b'\x03\xd5\x01\x00d\xca9\x9cvwC\x7f \x1b]j\xfb\xc5\xa3tA\xa1k.N9\x01\x08\x03\r\xcc\x02i\x01\xb7\x0b\xc2=\xc6\x92\xb1\rb\xe3\xbf\xdb;\x83\xb7\xbf{\xe2vk\xd1\xed\xef\xbf[d<{.n\x7fo\xd5WC\xab\x84\xeb\xecN\xd3#\xcc\xed\x0cAks\xa5\xe6\x87?\x15\xb3:\x8d\xfdS}\xb5O\x9a\xb8\x9d\xddK\x98\xf5+HSLq\x14\x84\xab:Fd!\xa8\xc0\x0c\xd2\xd3\x0eL\xd4\xec\x9b\xcb\xf2\xcf\xae\x18\xd9\x15&fg\xbf\xd6.```
There can be a couple of reasons for this error
I faced the same error. In my case, I am calling a put URL. In your case, it is post
I implemented my put API in such a way that, it won't return anything except status code. So after receiving the Response body contains no data. And we are trying to parse that to JSON. But actually nothing is there to parse.
I have some application which uses aiohttp.
I sent POST request into approptiate endpoint, e.g.:
POST mysite.com/someendpoind/
with data similar to:
{"param1": "value1", "param2": "value2", ..., "paramn": None}
Then on backend side, I want to add some additional conditional into this request:
data = await request.json()
data["additional_conditional"] = True
But request.json() fails with an error:
[ERROR] Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/aiohttp/web_protocol.py", line 422, in start
resp = yield from self._request_handler(request)
File "/usr/local/lib/python3.5/dist-packages/aiohttp/web.py", line 306, in _handle
resp = yield from handler(request)
File "/usr/local/lib/python3.5/dist-packages/aiohttp_session/__init__.py", line 129, in middleware
response = yield from handler(request)
File "/opt/bikeamp/auth/__init__.py", line 57, in wrapped
return (yield from f(request, user))
File "<my_module>.py", line 185, in <my_func>
data_json = await request.json()
File "/usr/local/lib/python3.5/dist-packages/aiohttp/web_request.py", line 469, in json
return loads(body)
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/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)
Then I decided to check somehow what is the content of my request:
await request.read()
b'field1=value1&field2=value2&field3=value3&field4=&field5=&field6='
So, I'm not sure, but the problem may be with empty parameters.
Also, I was trying to get this data via:
data = await request.post()
data["additional_condition"] = True
But this returns MultiDictProxy. Python can't pickle these objects.
Is there any known solutions?
I had the same issue, if post was something like {"email": "some#email.com"} check it with:
#router('/', methods=['POST', ])
async def post_request(request):
post = await request.post()
email = post.get('email') # because it's MultiDict
logging.warning(post) # see post details
logging.warning(email) # shows value "some#email.com"
json = await request.text() #
logging.warning(json) # shows json if it was ajax post request
I am trying to convert a dictionary object into json, but unable to do so. I tried json.load(), json.loads() but it gives me following error:
File "lean.py", line 252, in createJSON
return json.loads(jsonElementDict)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
When I print dictionary object, it's as follows:
{
'city': 'Pittsburgh',
'state': 'Pennsylvania',
'gross_floor_area': '',
'energyData': [
{
'cdd': '0.0',
'reading': '80.8',
'monthYear': 'Nov-2014'
},
{
'cdd': '0.0',
'reading': '300000.0',
'monthYear': 'Nov-2014'
}
]
}`
Code:
outputJSON = json.loads(jsonElementDict)
print outputJSON.json['city']