Question
Javascript API Net.HTTP.Request POST not working
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);
}
}
