Airtable / airtable.js

Airtable javascript client
MIT License
1.98k stars 404 forks source link

414 error when using long filterByFormula with single quotes #392

Open DiscretePython opened 10 months ago

DiscretePython commented 10 months ago

Runtime: NodeJS 18 Library Version: 0.12.2 Workaround: Use double quotes for strings in filterByFormula

I have a filterByFormula that is 13283 characters long when unencoded. It's a long OR(...) function with hundreds of RECORD_ID()='recXXXXXXXXX' comparisons. When I attempt to run select and pass this filter formula I am getting the following:

AirtableError { error: 'UNEXPECTED_ERROR', message: 'An unexpected error occurred', statusCode: 414 }

After some digging I found that the issue is being caused by a discrepancy in the URL encoding of the single quotes around the record ids. Here the function encodeURIComponent encodes the URL without encoding single quotes resulting in a length of 15010. However, when node-fetch makes the API call, it internally encodes the URL with the single quotes encoded resulting in a longer URL length above the 16k char limit, hence the 414 error.

Making the following change resolves the issue for me but there may be a better solution:

parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`); to parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value).replace(/'/g, "%27")}`);