How do you escape percent signs when making a HTTP web query to CartoDB

45 Views Asked by At

For example:

something like this works:

SELECT * FROM table_1 WHERE field_1 LIKE 'FOO_'

However this does not:

SELECT * FROM table_1 WHERE field_1 LIKE 'FOO%'

I've tried every escape sequence I can find. Tt either doesn't work or the HTML query interprets the % prior to the query.

2

There are 2 best solutions below

0
Shoaib Burq On BEST ANSWER

You need to wrap your query in the encodeURIComponent function

let query = encodeURIComponent(
  "select admin from public.ne_adm0_europe where admin like 'Ger%'"
)

let url = `https://cartojs-test.carto.com/api/v2/sql?q=${query}`

fetch(url)
  .then((response) => response.json())
  .then((myJson) => console.log(myJson));

0
Jasen On

did you try this:

SELECT * FROM table_1 WHERE field_1 LIKE 'FOO%25'