#๐Ÿ”’ getting a certain value from requests.json

24 messages ยท Page 1 of 1 (latest)

flat pond
#

im trying to get a certain value from my code when using requests and json but im getting a error can anyone help?

import requests
import json

token = "my token"

url = 'https://app.addy.io/api/v1/aliases'
payload = {
"domain": "anonaddy.me",
"description": "Made with script",
"format": "random_characters"
}
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}

response = requests.request('POST', url, headers=headers, json=payload)
data = response.json
email = data['data']['email']

print(response.json())
print(email)

proud groveBOT
#

@flat pond

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

flat pond
#

error:
Traceback (most recent call last):
File "c:\Users\littl\OneDrive\Documents\Methods\addy.py", line 20, in <module>
email = data['data']['email']
~~~~^^^^^^^^
TypeError: 'method' object is not subscriptable

royal needle
#

*data

flat pond
#

how would i call it?

royal needle
#

You call a function by putting () after its name:

print  # Not called
print()  # Called
flat pond
royal needle
#

Functions are just objects in Python. If you just write the name print for example, this becomes the print function. If you don't do anything with that function though, nothing happens:

#

!e

print  # Doesn't do anything
print(1)  # Prints 1
x = print  # Saves the print function object under the name 'x'
x(2)  # Prints 2 because 'x' is 'print'
proud groveBOT
royal needle
#

The last two lines show how, because functions are objects, they can just be assigned to names like any other objects,

#

Basically, if you want a function to run, you need to put () after its name.

flat pond
royal needle
#

Functions can be used like any other object though:

#

!e

print(print)
print(id(print))
help(print)
proud groveBOT
# royal needle !e ```python print(print) print(id(print)) help(print) ```

:white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | <built-in function print>
002 | 139869555035728
003 | Help on built-in function print in module builtins:
004 | 
005 | print(*args, sep=' ', end='\n', file=None, flush=False)
006 |     Prints the values to a stream, or to sys.stdout by default.
007 | 
008 |     sep
009 |       string inserted between values, default a space.
010 |     end
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/3HFGMBYLRVEPRR6G6SXL34OTVE

flat pond
royal needle
proud groveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.