Skip to main content
Participating Frequently
April 9, 2025
Question

Javascript API Net.HTTP.Request POST not working

  • April 9, 2025
  • 1 reply
  • 774 views

Hello,

 

I'm trying to send some information related to a pdf to an external API using a POST request, but I'm getting an error "A network connection could not be created".

 

I can run the post request from Postman without any issues.

 

Can you please let me know what I'm doing wrong?

 

Thanks

 

Code:

 

function test()
{
try 
{
                var doc = this;
 
        if (!doc) 
{
throw new Error('No active document found.');
        }
 
var docName = doc.documentFileName;
 
app.beginPriv();     
const tempFilePath = "/c/temp/tempDocument.pdf";
doc.saveAs(tempFilePath);
var docContent = util.readFileIntoStream(tempFilePath);
app.endPriv();    
 
        if (!docContent) 
{
throw new Error('Failed to retrieve document content as a stream.');
        }
 
var base64Stream = util.stringFromStream(docContent, "utf-8");
        
var params = 
{
cVerb: "POST",
aHeaders: [ {name: "Content-Type", value: "application/octet-stream"} ],
oRequest: util.streamFromString(JSON.stringify(
{
"docName": docName,
"content": base64Stream,
"docId": "12345"
})),
oHandler: 
{
response: function (msg, uri, e) 
{
app.alert("Error: " + e.text)
app.alert("File Saved");
            }
        }           
};
 
app.beginPriv();     
Net.HTTP.request(params);
    app.endPriv();
}
catch(error)
{
app.alert(error);
}
}

 

1 reply

Thom Parker
Community Expert
Community Expert
April 9, 2025

Is this code being run from a privileged context? such as the console window?

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
April 9, 2025

It's launched from a Folder environment, with privileges

Thom Parker
Community Expert
Community Expert
April 9, 2025

I ask because the "test()" function you define in the posted code is not trusted. 

The first thing I'd suggest is making the "test()" a trusted function. 

Next, try a much simpler operation, such as a GET that doesn't require any header info.

 

You might also be interested in this tool, which uses Net.HTTP:

https://www.pdfscripting.com/public/HTTP-Access-Tester-Description.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often