Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API in Python and Postman
#5
(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": ["2018­07-06 06:06", "2018­07­30 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:06
If 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"
}
Reply


Messages In This Thread
API in Python and Postman - by melo - Aug-28-2018, 08:38 AM
RE: API in Python and Postman - by gontajones - Aug-28-2018, 07:48 PM
RE: API in Python and Postman - by melo - Aug-29-2018, 08:17 AM
RE: API in Python and Postman - by gontajones - Aug-29-2018, 10:39 AM
RE: API in Python and Postman - by snippsat - Aug-29-2018, 12:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  upload image with generated code from Postman does not work magic7598 0 1,717 Nov-30-2019, 10:32 AM
Last Post: magic7598
  [split] API in Python and Postman fioranosnake 26 8,346 Nov-20-2019, 07:30 PM
Last Post: fioranosnake

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020