Skip to main content
BurtonDev
Inspiring
July 13, 2017
Question

How can I connect to an API and import JSON or XML data into a form field?

  • July 13, 2017
  • 5 replies
  • 4889 views

I referenced the Javascript documentation and tried using a Net.HTTP.request. The documentation only shows an example of how to do an MKCOL request, not a GET request.

If I hard-code the cURL value, I get the value "undefined". If I try passing it through a variable (as in the example below), I get the error "Invalid Arguments".

My code looks something like this:

foo = app.trustedFunction(function (cURL) {

    app.beginPriv();

    var params = {

       cVerb: "GET",

       cURL: cURL,

       oHandler: {

            response: function (msg, uri, e) {

                var stream = msg;

                var string = "";

                string = SOAP.stringFromStream(stream);

                console.println(string);

                console.println(stream);

            }

        }

    };

    Net.HTTP.request(params);

    app.endPriv();

});

foo("http:/api.somesite.com/data?q=param&apikey=123xyz&mode=xml");

(I didn't try passing data to the form yet, but just tested out the code by printing to the console first).

I am assuming that the variables in the response function (msg, uri, e) are populated by the response from the server? I am not quite sure how to "grab" the data that I want, and why I am getting these errors. Also wondering if I can do it without using SOAP. Thanks in advance!

This topic has been closed for replies.

5 replies

Participant
October 29, 2021

I think the problem lies which level you put your script. This gist, This makes a HTTP GET request from within Adobe Acrobat · GitHub, has a sample with a warning 'This file must be placed with the user JavaScript file location or the Acrobat JavaScript file location.', which is same as the note in the official SDK for Net.HTTP methods (Acrobat DC SDK Documentation (adobe.com)), 'This method can only be made outside the context of a document (for example, in a folder level JavaScript).'

Legend
March 3, 2021

"undefined" is not an error, it's a value, often normal. If you do get an error, please show your error messages in full (they give more info and give a line number - point us to the line given). If it's printed by itself, it's probably what is passed to your response callback.

 

The response from the request is passed to the function you define. So, msg, url and e are defined in that function - and nowhere else. If you want to read those values later you have to copy them somewhere.

BurtonDev
BurtonDevAuthor
Inspiring
August 12, 2017

Hold on a sec... The documentation that John referenced is from 2007, but Karl referenced the SDK documentation (which I didn't even know existed, LOL) from 2015. His article states that Net.HTTP requires a LiveCycle Extension to support in Acrobat Reader.

 

I found this article which expands on Karl's post regarding this matter: Adobe LiveCycle Designer: Working with PDF and Acrobat

 

It states:

Although Reader extending with Acrobat Professional is good for many cases, you’ll have to extend your forms with LiveCycle Reader Extensions if you need Reader users to access any of the following functionality in your XFA PDFs:

    • Connecting to data in real time
    • Using barcodes
    • Attaching files

 

Therefore, since I require the form to connect to data in real time, I won't be able to do this with Acrobat Pro DC. util only substitutes SOAP inside the Net.HTTP.request, so it will be subject to the requirements.

 

I will be looking into another solution right now, and once I do, I will come back and post again.

 

Thanks again Karl Heinz Kremer and John Frederick Chionglo for your answers, they have helped me better understand the issue, even if I can't resolve it

Karl Heinz  Kremer
Community Expert
Community Expert
August 14, 2017

You may have to create a server-side solution that can translate between your 3rd party solution (that uses SOAP) and XFDF (so that you can allow Reader  to retrieve and submit data).

Known Participant
August 10, 2017

If an error occurs then Net.HTTP.request still calls on the response handler. Try the following: 1. Include the check for error in your response then try again. 2. If you get an error type “NetworkError” then make sure the url is correct and there are no hidden characters or control characters in the url. The method util.stringFromStream is probably an alternative to SOAP.stringFromStream. Reference Adobe Systems Incorporated. (2007). Net.HTTP and util.stringFromStream (pp. 544 – 556; 743 – 744). In Adobe Acrobat SDK 8.1 JavaScript for Acrobat API Reference for Microsoft Windows and Mac OS. Edition 2.0, April 2007. San Jose, California: Adobe Systems Incorporated. Retrieved Aug. 3, 2010 from DC Developer Resources.

BurtonDev
BurtonDevAuthor
Inspiring
August 12, 2017

Thanks, John! I figured out the reason I was getting the error "undefined" - it was because I was missing a slash in the URL.

I tested out using util instead of SOAP and it works in Adobe Acrobat. I will get back to you again once I test it in Reader.

BurtonDev
BurtonDevAuthor
Inspiring
July 31, 2017

In three weeks with 45 views, no one has answered my post. I would really appreciate someone providing an answer, even if the answer is "it can't be done". I have not found a solution to my problem. Please help!

Karl Heinz  Kremer
Community Expert
Community Expert
August 8, 2017

Using the NET interface is not something a lot of people do, that's very likely why you have not received a response yet. Take a look here for at least another sample: Getting a Serial Number Into a Form Using SOAP - KHKonsulting LLC

If you want to do this without SOAP, I just published a new post about using XFDF to get data from a form or into a form: Connect to Database from PDF Form - This Time Without SOAP - KHKonsulting LLC

BurtonDev
BurtonDevAuthor
Inspiring
August 10, 2017

Hi Karl, thanks for your reply and for referring me to your posts!

Your example using XFDF looks very interesting.. However, it will not work for my situation. I'm not trying to connect to my own server, but a third-party server throught their API. So I won't be able to write php on their server.

Yes, I want to avoid using SOAP because I want the code to run in Adobe Reader, not just Acrobat, as your first post points out.

So is there really no one that can show me how to do this with the Net.HTTP.request?