HTTP response with both binary data and JSON












1














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;









share|improve this question
























  • See Multipart HTTP response
    – Phil
    Nov 21 '18 at 8:27










  • So the suggestion is to use the same format both ways with multipart? Same format as when sending a multipart to the server?
    – clarkk
    Nov 21 '18 at 8:30










  • Have you tried using CURL post?
    – Shail Paras
    Nov 21 '18 at 8:33
















1














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;









share|improve this question
























  • See Multipart HTTP response
    – Phil
    Nov 21 '18 at 8:27










  • So the suggestion is to use the same format both ways with multipart? Same format as when sending a multipart to the server?
    – clarkk
    Nov 21 '18 at 8:30










  • Have you tried using CURL post?
    – Shail Paras
    Nov 21 '18 at 8:33














1












1








1







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;









share|improve this question















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;






php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 11:57

























asked Nov 21 '18 at 8:25









clarkk

7,58645143231




7,58645143231












  • See Multipart HTTP response
    – Phil
    Nov 21 '18 at 8:27










  • So the suggestion is to use the same format both ways with multipart? Same format as when sending a multipart to the server?
    – clarkk
    Nov 21 '18 at 8:30










  • Have you tried using CURL post?
    – Shail Paras
    Nov 21 '18 at 8:33


















  • See Multipart HTTP response
    – Phil
    Nov 21 '18 at 8:27










  • So the suggestion is to use the same format both ways with multipart? Same format as when sending a multipart to the server?
    – clarkk
    Nov 21 '18 at 8:30










  • Have you tried using CURL post?
    – Shail Paras
    Nov 21 '18 at 8:33
















See Multipart HTTP response
– Phil
Nov 21 '18 at 8:27




See Multipart HTTP response
– Phil
Nov 21 '18 at 8:27












So the suggestion is to use the same format both ways with multipart? Same format as when sending a multipart to the server?
– clarkk
Nov 21 '18 at 8:30




So the suggestion is to use the same format both ways with multipart? Same format as when sending a multipart to the server?
– clarkk
Nov 21 '18 at 8:30












Have you tried using CURL post?
– Shail Paras
Nov 21 '18 at 8:33




Have you tried using CURL post?
– Shail Paras
Nov 21 '18 at 8:33












1 Answer
1






active

oldest

votes


















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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407860%2fhttp-response-with-both-binary-data-and-json%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer


























      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.






      share|improve this answer
























        1












        1








        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 8:31









        nix

        11325




        11325






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407860%2fhttp-response-with-both-binary-data-and-json%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen