Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to download in CSV using API #23037

Closed
vipera7 opened this issue May 30, 2022 · 1 comment
Closed

How to download in CSV using API #23037

vipera7 opened this issue May 30, 2022 · 1 comment
Labels
.Needs Triage Type:Question Please use the forum: https://discourse.metabase.com/

Comments

@vipera7
Copy link

vipera7 commented May 30, 2022

Hello,

I'm trying to download a report in csv using API.
I found the endoit (I hope it's the right one):

POST /api/dataset/:export-format
Execute a query and download the result data as a file in the specified format.

PARAMS:
export-format value must be one of: api, csv, json, xlsx.

query value must be a valid JSON string.

visualization_settings value must be a valid JSON string.

I wrote it in PHP :

public function DownloadCSV($token, $query, $exportFormat) {
    if($exportFormat == "api" or $exportFormat == "csv" or $exportFormat == "json" or $exportFormat == "xlsx"){
        $ch = curl_init();
	//https://github.com/metabase/metabase/blob/master/docs/api/dataset.md
	var_dump($this->MetabaseURL.'/api/dataset/'.$exportFormat);
	curl_setopt($ch, CURLOPT_URL, $this->MetabaseURL.'/api/dataset/'.$exportFormat);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
	curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query");

	$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("text.txt", $result);
	return $result;
 }
	 }

Output :

string 'http://localhost:3000/api/dataset/csv' (length=37)
string 'Malformed JSON in request body.' (length=31)

The query contain a SQL request.

How do I format query and visualization_settings ?

And i'll like to update the data before exporting, how can I update the data ?
Thanks

@vipera7 vipera7 added .Needs Triage Type:Question Please use the forum: https://discourse.metabase.com/ labels May 30, 2022
@flamber
Copy link
Contributor

flamber commented May 30, 2022

Please use the forum for questions and troubleshooting: https://discourse.metabase.com/

@flamber flamber closed this as completed May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.Needs Triage Type:Question Please use the forum: https://discourse.metabase.com/
Projects
None yet
Development

No branches or pull requests

2 participants