How to retrieve data from a specific REST endpoint. (prefer using Python)



  • My task from stretch:

    Retrieve sensor data from a specific REST endpoint. This would be achieved by using a HTTP GET issued against the following URL:

    https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000

    The format of the data provided by the REST ENDPOINT as below:

    50dbd127-2508-44c5-b167-aeb77189c798-image.png



  • Hey @Alan-Daniel

    I assume you wanted the first two items of blobJson. In that case you can do something like this:

    # importing the requests and ast library 
    import requests 
    import ast
      
    # api-endpoint 
    URL = "https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000"
      
    # sending get request and saving the response as response object 
    r = requests.get(url = URL) 
    # extracting data in json format 
    data = r.json()
    
    # Get value for blobJson 
    blobJson = data[0]["blobJson"]
    
    # Convert string to list 
    list = ast.literal_eval(blobJson)
    
    # Get first two items in list
    firstTwo = list[:2]
    
    # Print first two items of list
    print(firstTwo)
    

    Let me know if that worked for you.



  • Hey @Alan-Daniel

    How about something like this:

    # importing the requests library 
    import requests 
      
    # api-endpoint 
    URL = "https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000"
      
    # sending get request and saving the response as response object 
    r = requests.get(url = URL) 
      
    # extracting data in json format 
    data = r.json()
    
    # Printing the results 
    print(data)
    

    source: https://www.geeksforgeeks.org/get-post-requests-using-python/



  • Hi, Thanks. It works.
    Is there any way to print only the first 2 data from a specific REST endpoint (no the entire data) .

    which only 2 element in blobJson



  • Hey @Alan-Daniel

    I assume you wanted the first two items of blobJson. In that case you can do something like this:

    # importing the requests and ast library 
    import requests 
    import ast
      
    # api-endpoint 
    URL = "https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000"
      
    # sending get request and saving the response as response object 
    r = requests.get(url = URL) 
    # extracting data in json format 
    data = r.json()
    
    # Get value for blobJson 
    blobJson = data[0]["blobJson"]
    
    # Convert string to list 
    list = ast.literal_eval(blobJson)
    
    # Get first two items in list
    firstTwo = list[:2]
    
    # Print first two items of list
    print(firstTwo)
    

    Let me know if that worked for you.



  • @avan said in How to retrieve data from a specific REST endpoint. (prefer using Python):

    equests.get(url = URL)

    extracting data in json format

    data = r.json()

    Get value for blobJson

    blobJson = data[0]["blobJson"]

    Convert string to list

    yes, thanks! it works well!


Log in to reply