Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

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

    Python
    2
    5
    65
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Alan Daniel
      Alan Daniel last edited by

      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

      Reply Quote 0
        1 Reply Last reply

      • avan
        avan last edited by

        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.

        Reply Quote 0
          1 Reply Last reply

        • avan
          avan last edited by avan

          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/

          Reply Quote 1
            1 Reply Last reply

          • Alan Daniel
            Alan Daniel last edited by

            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

            Reply Quote 0
              1 Reply Last reply

            • avan
              avan last edited by

              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.

              Reply Quote 0
                1 Reply Last reply

              • Alan Daniel
                Alan Daniel last edited by

                @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!

                Reply Quote 0
                  1 Reply Last reply

                • First post
                  Last post