I am trying to fetch a file from REST API endpoint via POST request, and then return it to browser.
- User fills in the form, and requests for file download.
- controller forms a request body from form parameters, and calls REST API endpoint.
- API either sends a file directly or a json on case of failed resultset.
I am trying to get this file in the controller and then redirect it to client.
I am using Guzzle for this.
public function downloadMatchedSkusWithTags() {
$inputParams = [];
$inputParams['client_name'] = $this->request->input('dw_client_name');
$inputParams['from_date'] = $this->request->input('dw_from_date');
$inputParams['to_date'] = $this->request->input('dw_to_date');
$inputParams['tags'] = $this->request->input('dw_tags');
if(!empty($inputParams['tags']))
$inputParams['tags'] = implode("|", $inputParams['tags']);
$inputParams = array_map(array($this, 'sanitizeInputParams'), $inputParams);
$filterArr = array_filter($inputParams);
$client = new Client(["base_uri" => $this->apiBaseUrl]);
$url = 'download/filterdownload/';
try {
$response = $client->post($url, ['headers'=>['Content-Type' => 'application/json'], 'body'=>json_encode($filterArr)]);
$file = $response->getBody()->getContents();
return $file;
} catch (\Exception $e) {
return redirect()->back()->with('error', $e->getMessage());
}
if(!empty($resArr) && array_key_exists('response', $resArr))
return redirect()->back()->with('error', $resArr['response']);
}
Here's my controller method.
But this only redirects me to a blank page and no download.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire