Help

using f-string with filterByFormula

Topic Labels: Community
Solved
Jump to Solution
617 1
cancel
Showing results for 
Search instead for 
Did you mean: 
wesam
4 - Data Explorer
4 - Data Explorer

Hi,
i am using python-requests and trying to use f-string with filterByFormula here is a simple implementation:

hard coding the name worked:

 

 

 

url=f"https://api.airtable.com/v0/{base_id}/{table_name}/"
headers={'Authorization': f'Bearer {api_key}',
                        'Content-Type': 'application/json'}

query={"filterByFormula":"({full_name}="some name")"}
response=requests.get(url, headers=headers, params=query)
data=response.json()

 

 

 

 but when i tried to use f-string it didn't work. here is a a piece of code where the problem occurred:

 

 

 

url=f"https://api.airtable.com/v0/{base_id}/{table_name}/"
headers={'Authorization': f'Bearer {api_key}',
                        'Content-Type': 'application/json'}

employee_name="some name"
query={"filterByFormula":f"{{full_name}}={employee_name}"}
response=requests.get(url, headers=headers, params=query)
data=response.json()

 

 

 

{'error': {'type': 'INVALID_FILTER_BY_FORMULA',
  'message': 'The formula for filtering records is invalid: Invalid formula. Please check your formula text.'}}

i wish somebody can help me with this
thanks

1 Solution

Accepted Solutions
wesam
4 - Data Explorer
4 - Data Explorer

I've found my mistake and i will post it here for reference for others:

the wrong code:

query={"filterByFormula":f"{{full_name}}={employee_name}"}

the right code (use single quotes for the '{employee_name}'):

query={"filterByFormula":f"{{full_name}}='{employee_name}'"}

the parameter placeholder must be in single quotes.

See Solution in Thread

1 Reply 1
wesam
4 - Data Explorer
4 - Data Explorer

I've found my mistake and i will post it here for reference for others:

the wrong code:

query={"filterByFormula":f"{{full_name}}={employee_name}"}

the right code (use single quotes for the '{employee_name}'):

query={"filterByFormula":f"{{full_name}}='{employee_name}'"}

the parameter placeholder must be in single quotes.