Jan-21-2017, 07:46 AM
You should use Requests.
Data payload to server is a Python dictionary.
Requests has build in json decoder,it will do encoding inn and out of server(can just call
Eg:
Data payload to server is a Python dictionary.
Requests has build in json decoder,it will do encoding inn and out of server(can just call
json()
).Eg:
>>> import requests >>> data = {"device_name": "Super-pc", "Type": "DevicePort", "page_size": 2} >>> r = requests.post('http://httpbin.org/post', json=data) >>> r.status_code 200 >>> r.json() {'args': {}, 'data': '{"device_name": "Super-pc", "page_size": 2, "Type": "DevicePort"}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '65', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.9.1'}, 'json': {'Type': 'DevicePort', 'device_name': 'Super-pc', 'page_size': 2}, 'origin': '83.143.86.75', 'url': 'http://httpbin.org/post'} >>> data = r.json() >>> data['json'].get('device_name', 'Not on server') 'Super-pc' >>> data['json'].get('hello', 'Not on server') 'Not on server'