Javascript API Net.HTTP.Request POST not working
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
Is this code being run from a privileged context? such as the console window?
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
It's launched from a Folder environment, with privileges
Copy link to clipboard
Copied
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
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
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?
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I've added localhost as a trusted server, and got the same error 😞
Copy link to clipboard
Copied
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.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
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 😄

