4

I need to reponse to a client with both some binary data (a PDF file) and some additional data as JSON

How is this possible?

I can do this to send the PDF back to the client, but how to send the JSON string in the same response?

The call is done via an API so there is not a browser in the other end

header('Content-Type: '.$type);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: '.strlen($body));
echo $body;
clarkk
  • 27,151
  • 72
  • 200
  • 340

1 Answers1

1

You could use the multipart technique like in emails. One part is the JSON (text/json), the other part is the PDF (application/octet-stream a.k.a. binary).

Another possibility would be as a custom header (e.g. X-MyJSON), if the JSON string is small enough to fit into the header line. The PDF is unlikely to fit into a header string.

nix
  • 233
  • 3
  • 5