Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

To access a REST service within Python we strongly recommend the ‘requests’ module.

Code Block
languagepy
titleReading (GET request)
import requests

url = '%(schema)s://%(host)s:%(port)s/%(path)s?%(query)s'
properties = {
    'schema': 'http',
    'host':   'localhost',
    'port':   '8001',
    'path':   'HMI/sharp_cea_720s_nrt',
    'query':  'start=2016-01-19T00:00:00Z&end=2016-01-19T00:00:00Z&meta=false'
}

res = requests.get(url % properties)
resJson = res.json()
Code Block
languagepy
titleWriting (POST request)
import requests

my_data = {}
data = {
  'data': {},
  'lat_hg': 0,
  'long_carr': 0,
  'long_hg': 0,
  'time_start': '2016-08-26T06:29:01.424Z'
}

url = '%(schema)s://%(host)s:%(port)s/%(path)s'
properties = {
    'schema': 'http',
    'host':   'localhost',
    'port':   '8002',
    'path':   'region/myDataset'
}

headers = {'Content-Type': 'application/json'}
res = requests.post(url % properties, headers=headers, json=data)

...