0

I'm building a Slim API for an update tool. The goal of the whole thing is, after some checks,to respond to the client with a Zipfile. This works just fine. But I don't not only want to send the client the requested zipfile, in addition I also want to add some information gathered from a database. This information is a string.

Right now my working code to respond with zipfile looks like this :

// up until this point several validations were successfully run, nothing special....
$zip = fopen($dir, 'r');
$response->write("$zip ");
$stream = new \Slim\Http\Stream($zip); // create a stream instance for the response body
//$stream = new \Slim\Http\Stream([$zip, $clientData]); // create a stream instance for the response body
 //$response->getBody()->withStatus(200)->withBody("Hello, $name, ");

return $response->withHeader('Content-Type', 'application/force-download')
    ->withHeader('Content-Type', 'application/octet-stream')
    ->withHeader('Content-Type', 'application/download')
    ->withHeader('Content-Description', 'File Transfer')
    ->withHeader('Content-Transfer-Encoding', 'binary')
    ->withHeader('Expires', '0')
    ->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
    ->withHeader('key', $uuid)
    ->withBody($stream); // all stream contents will be sent to the response

How do I have to modify it to (in pseudo php ) do this :

$zip = fopen($dir, 'r');
$response->write("$zip ");
$stream = new \Slim\Http\Stream($zip); // create a stream instance for the response body
$queryResult = 'Some JSON as string';
$responseArray = [$stream, $queryResult];

return $response->withbody($responseArray);

I tried the above solution but it did not work correctly. I get a 500 error, but string still shows up on the website. What am I missing here?

jdoe
  • 99
  • 3
  • 14
  • 1
    I would recommend you to have 2 routes - one for retrieving zip file details and second just for retrieving zip file stream itself. If you want really use just one route, I believe you need to encode your zip/stream to Base64 and sent it as string – jDolba Oct 07 '19 at 22:37

1 Answers1

0

This cannot be done directly in one request.

You could send response with the database details and a link to the zip file within a JSON payload.

Another alternative is to dynamically create a new zip file containing a text file with the database details and the original zip file within it.

As another option, if you are using HTTP/2, you could send the database details and use Server Push via the Link header to push the zip file to the client.

Rob Allen
  • 12,643
  • 1
  • 40
  • 49