Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript API Net.HTTP.Request POST not working

Community Beginner ,
Apr 09, 2025 Apr 09, 2025

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);
}
}

 

TOPICS
Acrobat SDK and JavaScript
231
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 09, 2025 Apr 09, 2025

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

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 09, 2025 Apr 09, 2025

It's launched from a Folder environment, with privileges

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 09, 2025 Apr 09, 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 09, 2025 Apr 09, 2025

Also, it looks like the URL is the local HTTP server on a custom port. 

Are you sure the server is running and the specified port is available?  I suppose it is since you tested the URL with an external tool. So it is likely the issue is with Acrobat Security. 

Has the URL been added to the Acrobat trusted sites list?  Is "Protected mode" disabled?

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 10, 2025 Apr 10, 2025

The function is trusted, it's called from another script that makes it trusted, and I can make the GET request work without any issues.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 10, 2025 Apr 10, 2025

I've added localhost as a trusted server, and got the same error 😞

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 10, 2025 Apr 10, 2025

GET works, but POST fails.  So it's not the connection or a security issue. 

Have you tried a post using simpler data?  Just text for example?

 

Also, the code is sending Hex encoded text, I don't think "application/octet-stream" is appropiate as the content-type. But I'm not sure this really matters. 

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 11, 2025 Apr 11, 2025

Thanks for all your help Thom.

 

I've tried send a string in the oRequest but it says invalid parameter. When I convert it to a stream, and then change the header to text/plain it returns the connection error.

 

I think that my main problem is the debugging of the error, is there any way to improve the debugging that Adobe brings?

 

Also, oRequest needs to be a stream, isn't it?

 

Cheers

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2025 Apr 11, 2025

Yes, oRequest needs to be a stream.  I believe it converts the hex stream in to the appropiate data type for the actual transfer.   In your case the content type should probably be "application/json".   

I've used this object many times.  I've found that when all the paramters are correct it goes though, but if not, the error messages are not always meaningful. And there are few (read none) debug options inside the Acrobat JS model.  

For example, if I send a PDF in the body, and only a pdf, the the content type is "application/pdf".  Typically if more parameters are needed for a post like this they'll be added to either the URL query paramters or as a header entry.  

 

And you're not seeing anything hit the server script?  

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 15, 2025 Apr 15, 2025
LATEST

Hi Thom,

 

I managed to make it work, there was an issue between headers and what was expected in the body. 

Using text/plain and changing the controller made the trick.

 

Thanks a lot for your help 😄

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines