1

I need to make a python app to make a connection with the API

The python app is gonna send the user's ID to the API, then it's gonna get some informations about this user, such as their name, whether they are authenticated and so on.

I already know how to create the rest api, But I don't know how to consume it.

Thanks.

1 Answer 1

6

You can use requests. Use it like this:

# Get 
resp = requests.get('https://api.github.com/user', auth=('user', 'pass'))
json_response = resp.json()
# Response: {u'private_gists': 419, u'total_private_repos': 77, ...}
is_authenticated = json_response.get('is_authenticated', False)
if is_authenticated == True:
   # Do stuff

# Post
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
r.json()
# Similar response as above
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, Could you please show how to read the response and for example check some boolean information and then return the value.
Thanks again, But I still have one question. I didn't understand why You put false in the parentheses. I can't figure this out.
It means if json_response dictionary does not contain any parameter named is_authenticated, then it will return False as default value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.