Alex
Alex

Reputation: 625

How to consume api rest using TurboGears2?

I want to show the data in the view

r = requests.get('https://jsonplaceholder.typicode.com/posts')
print(r)
print(r.headers)
print(r.encoding)
data = r.json()
log.debug(data)
log.debug(r)

for post in data:
 s = format(post["id"],post['title'])

Any ideas?

Upvotes: 0

Views: 122

Answers (1)

neeraj
neeraj

Reputation: 89

Inside your web-app create a controller having @expose('json') as the decorator and then you can request that url the way you want. While requesting the url you may want to append .json at the end of the url. For example in your case

@expose('json')
def posts(self, *args, **kwargs):
    #do your stuff
    return dict(data=data)  

and then you can easily call this url.

r = requests.get('https://jsonplaceholder.typicode.com/posts.json')

Upvotes: 0

Related Questions