Coder Social home page Coder Social logo

queryParameters about carto-react HOT 7 CLOSED

cartodb avatar cartodb commented on June 22, 2024
queryParameters

from carto-react.

Comments (7)

jantolg avatar jantolg commented on June 22, 2024 1

@Josmorsot is ocurring with POST requests. FYI: I'm using v1.4.5

 executeSQL({
    query: 'SELECT 1',
    credentials,
    connection: 'carto_dw',
  })

from carto-react.

Josmorsot avatar Josmorsot commented on June 22, 2024

Related with #524. Fixed in #525

from carto-react.

Josmorsot avatar Josmorsot commented on June 22, 2024

v1.4.4 released

from carto-react.

jantolg avatar jantolg commented on June 22, 2024

@Josmorsot I think is not resolved.

The problem ocurrs when is not include any queryParameters, function will include by default [], the evaluation will be true and this one will include queryParameters: '[]', in case queryParemeters will be false, null or undefined, evaluation will return undefined and this will be included in queryParamseters as queryParameters: undefined . Both cases will return error from API

Source: https://github.com/CartoDB/carto-react/blob/master/packages/react-api/src/api/SQL.js:

function createRequest({ credentials, connection, query, opts = {}, queryParameters = [] }) {
  const { abortController, ...otherOptions } = opts;

  const { apiVersion = API_VERSIONS.V2 } = credentials;

  const rawParams = {
    client: CLIENT,
    q: query?.trim(),
    ...otherOptions,
    queryParameters: queryParameters ? JSON.stringify(queryParameters) : undefined
  };

[.....]

}

I think an approach could be:

/**
 * Create an 'SQL query' request
 * (using GET or POST request, depending on url size)
 */
function createRequest({ credentials, connection, query, opts = {}, queryParameters = null }) {
  const { abortController, ...otherOptions } = opts;

  const { apiVersion = API_VERSIONS.V2 } = credentials;

  const rawParams = {
    client: CLIENT,
    q: query?.trim(),
    ...otherOptions,
    ...(queryParameters && {queryParameters: JSON.stringify(queryParameters)})
  };

  if (apiVersion === API_VERSIONS.V3) {
    rawParams.access_token = credentials.accessToken;
  } else if (apiVersion === API_VERSIONS.V1 || apiVersion === API_VERSIONS.V2) {
    rawParams.api_key = credentials.apiKey;
  }

  const requestOpts = { ...otherOptions };
  if (abortController) {
    requestOpts['signal'] = abortController.signal;
  }

  // Get request
  const urlParamsForGet = Object.entries(rawParams).map(([key, value]) =>
    encodeParameter(key, value)
  );
  const getUrl = generateApiUrl({
    credentials,
    connection,
    parameters: urlParamsForGet
  });
  if (getUrl.length < REQUEST_GET_MAX_URL_LENGTH) {
    return getRequest(getUrl, requestOpts);
  }

  // Post request
  const urlParamsForPost =
    apiVersion === API_VERSIONS.V3
      ? [`access_token=${credentials.accessToken}`, `client=${CLIENT}`]
      : null;
  const postUrl = generateApiUrl({
    credentials,
    connection,
    parameters: urlParamsForPost
  });
  return postRequest(postUrl, rawParams, requestOpts);
}

from carto-react.

Josmorsot avatar Josmorsot commented on June 22, 2024

Hi @jantolg, is occurring with request through GET or POST? Can you share an example?

from carto-react.

Josmorsot avatar Josmorsot commented on June 22, 2024

I've just tried with:

executeSQL({
    query: 'SELECT 1',
    credentials,
    connection: 'bqconn',
  });

GET => https://gcp-us-east1.api.carto.com/v3/sql/bqconn/query?client=carto-react&q=SELECT%201&queryParameters=%5B%5D&access_token=

Response =>
image

CURL => curl 'https://gcp-us-east1.api.carto.com/v3/sql/bqconn/query?client=carto-react&q=SELECT%201&queryParameters=%5B%5D&access_token=<TOKEN>' \ -H 'authority: gcp-us-east1.api.carto.com' \ -H 'accept: application/json' \ -H 'accept-language: en-GB,en-US;q=0.9,en;q=0.8' \ -H 'cache-control: no-cache' \ -H 'origin: https://127.0.0.1:3000' \ -H 'pragma: no-cache' \ -H 'referer: https://127.0.0.1:3000/' \ -H 'sec-ch-ua: "Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"' \ -H 'sec-ch-ua-mobile: ?0' \ -H 'sec-ch-ua-platform: "macOS"' \ -H 'sec-fetch-dest: empty' \ -H 'sec-fetch-mode: cors' \ -H 'sec-fetch-site: cross-site' \ -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36' \ --compressed

It works fine for me (with v1.4.5 and v1.4.6). Can you try to reproduce with latest version and share a curl or create a repo with an example?

from carto-react.

Josmorsot avatar Josmorsot commented on June 22, 2024

Fixed in v1.4.7

from carto-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.