Using REST API and Python to retrieve data [retired]

Hi everybody,

I have some problem with my Python code which I use to retrieve data from the server using REST API.
Here’s my code:

import httplib

conn = httplib.HTTPConnection("www.thethingsnetwork.org")
conn.request("GET", "/api/v0/nodes/03FF8124")
res = conn.getresponse()
print res.status, res.reason

conn.close()

I want to get the latest data from node 03FF8124.
When I execute this I get the 301 error: ‘Moved Permanently’.
Is there anybody with a suggestion?

Kind regards,

Joeri

leave out the “www” ?

I find out that even I let away the second argument of the request function I get the 301 error.
When I set the host to thethingsnetwork.org I get 200: ‘OK’.
Thanks for that!

Still when I use the ‘/api/v0/nodes/03FF8124’ in the request argument I get 301.

More suggestions?

EDIT: I solved the problem an extra slash was needed at the end of the argument thus:

/api/v0/nodes/03FF8124/

for example:

/api/v0/nodes/03FF8124/?limit=1

Glad you found it!

For future debugging on 301 errors: print the final URL and visit using your browser -> you’ll see the redirect happen. Or debug the headers returned by the webserver (they contain the redirect).

1 Like

@joeri_01
Hi Joeri,
Is it possible to read data using pandas.read_json from this Rest API?
Have you tried it with your data?
I have some data coming into my TTN console and getting stored on the default TTN Data Storage, see screenshot below:
Swagger

If i try to read the json file from my swagger page using import requests into a dictionary and then putting that into a pandas dataframe. I get a JSONDecodeError: Expecting value: line 1 column 1 (char 0)
You can see my Jupyter Notebook in this link
Can you please provide me any solution to read this data on the Swagger page into a pandas dataframe?

  • I deleted your same question 1x is enough :sunglasses:

Okay, i found out how to read the data from TTN’s Swagger API in Python :+1:and wanted to share it with the TTN community :innocent:as i am sure lot of people use python for data analytics/visualization.

You basically feed the Curl link in the TTN Swagger API page to this website (https://curl.trillworks.com/) and it will give you the required python code to parse the data using the python package import requests.
Remember to use import pandas as pd
and then you can parse the response into a pandas dataframe like so:
df = pd.DataFrame.from_dict(response.json( ))

1 Like

Can anyone advise, does this method of retrieving data using python still work? All I get is error 301 even though I’ve including all the things learnt along the way.

I’m trying to use this in along with the Storage Integration to have the TTN retain the data for 7 days allowing this code to run every few hours.

import httplib
conn = httplib.HTTPConnection("thethingsnetwork.org")
conn.request("GET", "/api/v0/nodes/03FF8124/")
res = conn.getresponse()
print res.status, res.reason
conn.close()

HTTP 301 is not an error, but tells you that the location has changed:

print res.status, res.reason, res.getheader('Location')

In this specific case, TTN is redirecting you to the HTTPS URL:

301 Moved Permanently https://www.thethingsnetwork.org/api/v0/nodes/03FF8124/

That said: the new URL won’t work either, as the REST API is no more. You’ll have to use an alternative, like MQTT: A Python program to listen to your devices with MQTT. The storage integration has its own API; https://www.thethingsnetwork.org/docs/applications/storage/api.html

Thank you, @arjanvanb just the explanation I was looking for.