Using requests for API

Moving from [Tutorial] Using cURL for REST and RPC API calls - #4 by MartinHBramwell to keep that tutorial post clean.

For example the cURL for get:

curl -X GET https://{your frappe instance}/api/resource/Customer?fields=["name","address"]&filters=[["Customer","phone","=","4915227058038"]]

How to write it in requests as separate parameters?

url = "https://{your frappe instance}/api/resource/Customer"
headers = {"Authorization": "token xxx:yyy"}		
fields = ["name","address"]   #??
filters = [["Customer","phone","=","4915227058038"]]   #??
			
r = requests.get(url, headers=headers, params=fields, data=filters)  #??

OK get it. It’s:

payload = {
    "fields": '["name","address"]',
    "filters": '[["Customer","phone","=","4915227058038"]]'
}

then

r = requests.get(url, headers=headers, params=fields, params=payload)

The r.json or r.text return is:

{'data': [{'name': 'd8282655a6', 'address': 'street 123'}]}

Now, how do I get the value of name and address?

Using:

obj = json.loads(r.text)
print(obj['name'])

gives error because the key is within the data.

After several seek and try I get this:

print(obj['data'][0]['name'])

will return

d8282655a6

I can use this, but I don’t know if this is the right or efficient way to do it.
If anyone has any comments or inputs, please do so.

Thank you.

That is how I would do it.