Search code examples
htmlpostgresqlcartodb

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


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.


Solution

  • 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));