5 Replies Latest reply on Nov 22, 2011 10:55 AM by jack_freud

    How to send a document in net.HTTP.request as a POST variable?

    jack_freud

      I have read and reread the doc on net.HTTP.request, and it says I can pass an argument of oRequest, but I have not succeeded at passing in the current document, which is what I want to do.

       

      I've tried app.activeDocs[0], or encoding that to a string, but though it makes the call without error, nothing is sent.

       

      Should oRequest be simply a document, or a variable made up of name-value pairs? How do we send multiple parameters to a POST otherwise?

       

      I've succeeded fine with calling using GET, but when I tried to pass the document as a GET variable, I got nothing either.

       

      I'm sending to a LiveCycle REST end point so either POST or GET is fine.

       

      Thanks!

        • 1. Re: How to send a document in net.HTTP.request as a POST variable?
          George_Johnson MVP & Adobe Community Professional

          Good question. It seems that you'd need a method to create a stream object from the current document, but I don't see that there's a method for doing that. There may be one present somewhere, but that it's undocumented. Whenever there are features like this that are so inadequately documented, I suspect that Adobe doesn't really intend for the general public to use it and that it's really there to implement some feature(s) of Acrobat. Good luck.

          • 2. Re: How to send a document in net.HTTP.request as a POST variable?
            jack_freud Level 1

            Thanks for replying.

             

            To turn it into a stream I've tried Collab.documentToStream(app.activeDocs[0]).

             

            The receiving web app (Livecycle) gets 0 bytes.

             

            Forgetting the document aspect of my question, can you tell me how to structure the the oRequest object to send in a POST call of Net.HTTP.request()?  If I had any working example of that maybe I could move the rest forward.

             

            Thanks so much.

            • 3. Re: How to send a document in net.HTTP.request as a POST variable?
              George_Johnson MVP & Adobe Community Professional

              Does this work: Collab.documentToStream(this);

               

              You can try sending a simple string like this:

               

              oRequest = util.streamFromString("This is a test.");

              • 4. Re: How to send a document in net.HTTP.request as a POST variable?
                Dave Merchant MVP & Adobe Community Professional

                POSTing the current document is straightforward *provided* your script is running outside the document context (i.e. in the console or a folder-level script). There's no access from doc-level scripts for obvious reasons.

                 

                For example, putting this in the JS console will POST the current file to a URL:

                 

                Net.HTTP.request({

                  cVerb:'POST',

                  cURL: 'http://localhost/submit.php',

                  oRequest: Collab.documentToStream(this),

                  oHandler: {

                    response: function (msg, uri, err) {

                       if (err!=undefined) app.alert(msg);

                    }

                  }

                });

                 

                Your problem with talking to a vanilla webservice is that you cannot send the RFC1867 delimeters (i.e. form a "multipart/form-data" request) as the ReadStream object doesn't have a concatenation method in the Acrobat JSAPI. That's also how you would usually prepend other form fields, but if you're careful with your server-side coding you can send those as GET parameters in cURL. Normally you'd be polite and pass a Context-Type header in the aHeaders array - it won't matter to the transmission process but will allow your webservice to predict what the heck it's been sent.

                 

                Your webservice will then have to read the raw data. In PHP for example, you won't get anything in the $_FILES global but you can grab the PDF by reading everything from the input buffer, with the line:

                 

                $pdf = file_get_contents("php://input");

                 

                If that line was in the server script that the above JS code was aimed at, the variable $pdf will contain an exact binary copy of the currently-open document.

                • 5. Re: How to send a document in net.HTTP.request as a POST variable?
                  jack_freud Level 1

                  Thank you George, and thank you Dave. I was on the right track but was on the wrong track, trying to turn the result of Collab.documentToStream() into a string so I could pass it as a GET variable. I really wanted a POST and this works so far. Only change was not to use "this" but "app.activeDocuments[0]" since it's a folder script.

                   

                  Thanks once again!