Send parameters in body with export-format

Hello everyone,

I know that this subject has been discussed in other threads

and I tried to follow them as well as using the Network-tab and copying the cURL option while using the Metabase UI.

However, I think I'm still missing something.

I'm trying to request /api/card/:cardid/query/json but, as it's mentioned in the other questions, my parameters got ignored.

Here it is my cURL request (I tried without the format and, it worked):

curl 'https://webapp-metabase.azurewebsites.net/api/card/1/query/json' \
  -H 'Accept: application/json' \
  -H 'Accept-Language: en-US,en;q=0.9,es;q=0.8' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'Cookie: metabase.DEVICE=REDACTED; _ga=GA1.3.406897238.1663058564; metabase.TIMEOUT=alive; metabase.SESSION=REDACTED; _gid=GA1.3.97398548.1663688080; _gat=1' \
  -H 'Origin: https://webapp-metabase.azurewebsites.net' \
  -H 'Referer: https://webapp-metabase.azurewebsites.net/question/1-fleet-traffic?%3Flimit=1&start_date=2021-08-01&end_date=2021-08-31' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36' \
  -H 'sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Linux"' \
  --data-raw '{"ignore_cache":true,"collection_preview":false,"parameters":[{"type":"number/=","value":["1"],"target":["variable",["template-tag","limit"]]},{"type":"date/single","value":"2021-08-01","target":["variable",["template-tag","start_date"]]},{"type":"date/single","value":"2021-08-31","target":["variable",["template-tag","end_date"]]}]}' \
  --compressed

It's probably something silly but, can anyone help me with this?

Thank you all in advance!

Hi @fserrey
I'm unsure I understand your question. What are you trying to do?
If you can do it in Metabase, then you can do it via the API.

Hi @flamber
Sure, what I'm trying to do is to access the JSON format of a query result. I'm running the cURL command I pasted below but, the command ignore the parameters. However, if i use the same endpoint without the export-format JSON, it works perfectly.

So, how can i get the JSON format using the API together with the parameters as body?

Thanks!

@fserrey I must be missing something, so you are saying that it works correctly via the UI, but you cannot get it to work via the API?
Then post your full API code, because the Metabase UI just sends API requests, so if it works in the UI, then the exact same query will also work for you (example using cURL).

Hi @flamber, I'm sorry about the confusing message. I will try to explain everything from scratch. This is what I see when I request the query with the parameters when using the UI:


Additionally, if I just copy the query as cURL, this is what I get:

curl 'https://webapp-metabase.azurewebsites.net/api/card/1/query' \
  -H 'Accept: application/json' \
  -H 'Accept-Language: en-US,en;q=0.9,es;q=0.8' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'Cookie: metabase.DEVICE=REDACTED; _ga=GA1.3.406897238.1663058564; metabase.TIMEOUT=alive; metabase.SESSION=REDACTED; _gid=GA1.3.97398548.1663688080; _gat=1' \
  -H 'Origin: https://webapp-metabase.azurewebsites.net' \
  -H 'Referer: https://webapp-metabase.azurewebsites.net/question/1-traffic?%3Flimit=1&start_date=2021-08-01&end_date=2021-08-31' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36' \
  -H 'sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Linux"' \
  --data-raw '{"ignore_cache":false,"collection_preview":false,"parameters":[{"type":"number/=","value":["1"],"target":["variable",["template-tag","limit"]],"id":"2c8a7b83-d046-4ddd-6d66-1b54b59d703f"},{"type":"date/single","value":"2021-08-01","target":["variable",["template-tag","start_date"]],"id":"fd6b41e4-31c4-91f9-b383-4255b450827f"},{"type":"date/single","value":"2021-08-31","target":["variable",["template-tag","end_date"]],"id":"28fd9b91-5cde-bb6e-0a71-f14c877a1608"}]}' \
  --compressed

Until here, everything works fine. However, I'm trying to build the following request:

So I can get a JSON response with the data filtered. Therefore, following the previous cURL, I'm building the following cURL request in order to try the parameters as a body:

curl 'https://webapp-metabase.azurewebsites.net/api/card/1/query/json' \
  -H 'Accept: application/json' \
  -H 'Accept-Language: en-US,en;q=0.9,es;q=0.8' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'Cookie: metabase.DEVICE=REDACTED; _ga=GA1.3.406897238.1663058564; metabase.TIMEOUT=alive; metabase.SESSION=REDACTED; _gid=GA1.3.97398548.1663688080; _gat=1' \
  -H 'Origin: https://webapp-metabase.azurewebsites.net' \
  -H 'Referer: https://webapp-metabase.azurewebsites.net/question/1-traffic?%3Flimit=1&start_date=2021-08-01&end_date=2021-08-31' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36' \
  -H 'sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Linux"' \
  --data-raw '{"ignore_cache":true,"collection_preview":false,"parameters":[{"type":"number/=","value":["1"],"target":["variable",["template-tag","limit"]]},{"type":"date/single","value":"2021-08-01","target":["variable",["template-tag","start_date"]]},{"type":"date/single","value":"2021-08-31","target":["variable",["template-tag","end_date"]]}]}' \
  --compressed

But, when I get the response, it's not filtered by the parameters (they are ignored). The only thing I tried to remove are the id associated to each of the parameters as I think I cannot get them before making the POST request while using the API.

The method I'm trying to reproduce is this one.

I believe there is something that I'm missing in between and I hope that you can help me here. Thanks a lot!

@fserrey You are referencing two separate API endpoints. They work differently.

Do what you are trying to do in Metabase, meaning click the export button in bottom-right > select JSON. Then look at the request, copy it as cURL.

This is exactly what I repeatedly try to explain in all the other forum topics you are referencing as well.
Read this specifically again: API POST /api/card/:card-id/query/:export-format with parameters - #2 by flamber