Hi,
I'm struggling to generate a csv file using api. My report has two variables of type date (start and end).
Here is my function :
function DownloadCSV($token, $card, $exportFormat) {
if($exportFormat == "api" or $exportFormat == "csv" or $exportFormat == "json" or $exportFormat == "xlsx"){
$ch = curl_init();
$payload = json_encode(
array(
"parameters" =>
array(
"type" => "date/single",
"value" => "2023-01-01",
"target" => array(
"variable" => ["template-tag","start"]
)
),
array(
"type" => "date/single",
"value" => "2023-01-31",
"target" => array(
"variable" => ["template-tag","end"]
)
)
)
);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt($ch, CURLOPT_POSTFIELDS, "parameters=%5B%7B%22type%22%3A%22date%2Fsingle%22%2C%22value%22%3A%222023-01-01%22%2C%22target%22%3A%5B%22dimension%22%2C%5B%22template-tag%22%2C%22period_start%22%5D%5D%2C%22id%22%3A%228192689b-6579-aa3d-1b3b-d3852675e680%22%7D%5D");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_URL, 'http://localhost:3000/api/card/'.$card.'/query/'.$exportFormat);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Metabase-Session: '.$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
file_put_contents("export_".$card.".".$exportFormat, $result);
return $result;
}
}
Error :
Malformed JSON in request body.
And
{"database_id":2,"started_at":"2023-02-16T15:39:01.0416335+01:00","via":[{"status":"failed","class":"class clojure.lang.ExceptionInfo","error":"Error determining value for parameter "start": You'll need to pick a value for 'Start' before this query can run.","stacktrace":["-->
How can I retrieve csv content ?
I'm passing exactly chrome payload.