Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP response with both binary data and JSON

Tags:

php

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;
like image 474
clarkk Avatar asked Dec 31 '25 04:12

clarkk


1 Answers

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.

like image 184
nix Avatar answered Jan 01 '26 16:01

nix