Aug-29-2018, 12:43 PM
(Aug-29-2018, 08:17 AM)melo Wrote: I actually found the solution yesterday! the code bellow is working for people who are interested:Now you not sending JSON,your first post look more correct(you send JSON),but it can also depend on the server.
Requests has for long time had
json
parameter supported.Can also drop
Content-Type
,Requests will fix it.So the shorter version.
>>> import requests >>> >>> response = requests.post('http://httpbin.org/post', json={'Python': 'Forum'}) >>> response.status_code 200 >>> response.json() {'args': {}, 'data': '{"Python": "Forum"}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '19', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.19.1'}, 'json': {'Python': 'Forum'}, 'origin': '95.143.193.192', 'url': 'http://httpbin.org/post'} >>> response.json()['json'] {'Python': 'Forum'} >>> response.json()['json']['Python'] 'Forum'httpbin is test server for HTTP Request & Response Service.
Can test with your data.
import requests headers = {'Content-type': 'application/json'} data = { "structures_names": ["ENERGIE AGENCE - POST LU"], "dates": ["201807-06 06:06", "20180730 06:06"], "IO_name": ["temp bureau minerais 1 etage 3"], "IO_type": ["TEMPERATURE"], "period_length": ["year", "month"], } response = requests.post('http://httpbin.org/post', json=data, timeout=5)Test server response.
>>> response.status_code 200 >>> response.json() {'args': {}, 'data': '{"structures_names": ["ENERGIE AGENCE - POST LU"], "dates": ' '["2018\\u00ad07-06 06:06", "2018\\u00ad07\\u00ad30 06:06"], ' '"IO_name": ["temp bureau minerais 1 etage 3"], "IO_type": ' '["TEMPERATURE"], "period_length": ["year", "month"]}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '227', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.19.1'}, 'json': {'IO_name': ['temp bureau minerais 1 etage 3'], 'IO_type': ['TEMPERATURE'], 'dates': ['2018\xad07-06 06:06', '2018\xad07\xad30 06:06'], 'period_length': ['year', 'month'], 'structures_names': ['ENERGIE AGENCE - POST LU']}, 'origin': '85.165.177.151', 'url': 'http://httpbin.org/post'} >>> print(response.json()['json']['dates'][0]) 2018-07-06 06:06If look at
text
on server we similar payload that you send(should not be needed to send this).>>> print(response.text) { "args": {}, "data": "{\"structures_names\": [\"ENERGIE AGENCE - POST LU\"], \"dates\": [\"2018\\u00ad07\\u00ad06 06:06\", \"2018\\u00ad07\\u00ad30 06:06\"], \"IO_name\": [\"temp bureau minerais 1 etage 3\"], \"IO_type\": [\"TEMPERATURE\"], \"period_length\": [\"year\", \"month\"]}", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "232", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "python-requests/2.19.1" }, "json": { "IO_name": [ "temp bureau minerais 1 etage 3" ], "IO_type": [ "TEMPERATURE" ], "dates": [ "2018\u00ad07\u00ad06 06:06", "2018\u00ad07\u00ad30 06:06" ], "period_length": [ "year", "month" ], "structures_names": [ "ENERGIE AGENCE - POST LU" ] }, "origin": "95.143.193.192", "url": "http://httpbin.org/post" }