Node SOAP client to connect to InDesign Server - node.js

I am trying to get node/express to send soap request to indesign server.
Posting the request from Soap.ui or Postman works fine. Loading the "soap" page in the browser errors.
I also tried the node client with a few sample scripts from the new and they work, so the install should be OK.
This is what I have so far:
router.get('/soap', function(req, res, next) {
var url = 'http://<server_ip>:8088/service?wsdl';
var args = { "IDSP:RunScriptParameters" :
{ 'scriptLanguage': 'javascript',
'scriptFile': 'C:/indesign_scripts/test.jsx'
}
};
soap.createClient(url, function(err, client){
client.Service.Service.RunScript(args, function(err, result) {
if (err) console.log(err);
console.log(result);
});
});
client.describe() returns:
{ Service:
{ Service:
{ RunScript: [Object],
BeginSession: [Object],
EndSession: [Object] } } }
I am trying to use RunScript object.
client.describe().Service.Service.RunScript:
{ input:
{ runScriptParameters:
{ scriptText: 'xsd:string',
scriptLanguage: 'xsd:string',
scriptFile: 'xsd:string',
'scriptArgs[]': [Object],
targetNSAlias: 'IDSP',
targetNamespace: 'http://ns.adobe.com/InDesign/soap/' } },
output:
{ errorNumber: 'xsd:int',
errorString: 'xsd:string',
scriptResult:
{ data: 'xsd:anyType',
targetNSAlias: 'IDSP',
targetNamespace: 'http://ns.adobe.com/InDesign/soap/' } } }
Console shows this error:
[Error: connect ECONNREFUSED 127.0.0.1:8088]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8088 }
Indesign Server wsdl could be viewed here:
https://gist.github.com/tomtaylor/1034317
I suspect this is something with args variable format.

You can fix this issue by adding line below
client.setEndpoint('http://<server_ip>:8088');

I tried to add "Access-Control-Allow-Origin" to my headers in my app file, as suggested by #Chirdeep Tomar, but I am still getting the same errors.
The workaround I came up with was to use http request or curl for ajax post.
The example with request:
var express = require('express');
var request = require('request');
var parser = require('xml2json');
var router = express.Router();
router.get('/ProductionBooks/:id', function(req, res) {
var myId = req.params.id;
var myBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://ns.adobe.com/InDesign/soap/"><soapenv:Body>'
+'<soap:RunScript>'
+'<runScriptParameters>'
+'<scriptLanguage>javascript</scriptLanguage>'
+'<scriptFile>C:/indesign_scripts/test.jsx</scriptFile>'
+'</runScriptParameters>'
+'</soap:RunScript>'
+'</soapenv:Body>'
+'</soapenv:Envelope>';
request({
url: 'http://192.168.0.129:8088', //URL to hit
method: 'POST',
headers: {
'Content-Type': 'application/xml',
'Content-Length': Buffer.byteLength(myBody)
},
body: myBody
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode + '\n');
var objJSON = JSON.parse(parser.toJson(body));
console.log(objJSON['SOAP-ENV:Envelope']['SOAP-ENV:Body']['IDSP:RunScriptResponse'].errorNumber);
}
});
res.end();
});
Example with curl:
var curl = require("curl");
curl.post(url, soapBody, options, function(err, response, body) {
try {
console.log(response.body);
}
catch (err) {
console.log(err);
}
});
res.end();
});

using easy-soap-request:
const soapRequest = require('easy-soap-request');
const url = 'http://[YOUR SERVER ADDRESS]:[IDS PORT]/service?wsdl';
const sampleHeaders = {
'user-agent': 'sampleTest',
'Content-Type': 'text/xml;charset=UTF-8',
};
const xml = '<?xml version="1.0" encoding="UTF-8"?>'
+'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:IDSP="http://ns.adobe.com/InDesign/soap/">'
+'<SOAP-ENV:Body><IDSP:RunScript><IDSP:runScriptParameters>'
+'<IDSP:scriptLanguage>javascript</IDSP:scriptLanguage>'
+'<IDSP:scriptFile>[PATH TO SCRIPTS]/Hello.jsx</IDSP:scriptFile>'
+'<IDSP:scriptArgs><IDSP:name>myArg</IDSP:name><IDSP:value>Test Value</IDSP:value></IDSP:scriptArgs>'
+'</IDSP:runScriptParameters></IDSP:RunScript>'
+'<IDSP:RunScriptResponse><errorNumber>0</errorNumber><scriptResult><data xsi:type="IDSP:List"><item><data xsi:type="xsd:string">1</data></item></data></scriptResult></IDSP:RunScriptResponse>'
+'</SOAP-ENV:Body></SOAP-ENV:Envelope>';
// usage of module
(async () => {
const { response } = await soapRequest({ url: url, headers: sampleHeaders, xml: xml, timeout: 1000 }); // Optional timeout parameter(milliseconds)
const { headers, body, statusCode } = response;
console.log(headers);
console.log(body);
console.log(statusCode);
})();
Sample script (hello.jsx):
var arg = app.scriptArgs.get("myArg");
var res = "Your input: " + arg;
res;

Related

Convert AJAX request into HTTP request in nodejs

Actually i am using ajax call to connect to a 3rd party api when i am using the api on browser and i am able to get the data from the api.
Here is the AJAX code:-
var settings = {
async: true,
//crossDomain: true,
url: "https://rest.cricketapi.com/rest/v2/schedule/?access_token=XXX"
//"url": "https://rest.cricketapi.com/rest/v2/match/indaus_2020_one-day_02/?access_token=XXX"
//bblt20_2019_g28
};
//create token for the api for 24hours
let token;
function getToken() {
$.ajax({
type: "POST",
url: "https://rest.cricketapi.com/rest/v2/auth/",
data: {
access_key: "********************************",
secret_key: "********************************",
app_id: "http://localhost:8000/",
device_id: "developer"
},
success: function(data) {
console.log(data);
token = data.auth.access_token;
console.log(token,);
//do something when request is successfull
createUpcomingMatchesSchedule();
},
dataType: "json"
});
}
function createUpcomingMatchesSchedule() {
var urlJson = settings.url;
urlJson = urlJson.replace(/XXX/g, token);
settings.url = urlJson;
$.ajax(settings).done(function(response) {
console.log(response);
toGetUpcomingMatches(response);
});
}
Now i want to convert this ajax method into http request so that i can use the data on the server using nodejs.
Here is the code:-
const { db } = require("../../utility/admin");
var request = require("request");
// //*************************** API INITIALIZATION ********************************
var settings = {
async: true,
//crossDomain: true,
url: "https://rest.cricketapi.com/rest/v2/schedule/?access_token=XXX"
//"url": "https://rest.cricketapi.com/rest/v2/match/indaus_2020_one-day_02/?access_token=XXX"
//bblt20_2019_g28
};
var options = {
method: "POST",
// hostname: "https://rest.cricketapi.com",
uri: "https://rest.cricketapi.com/rest/v2/auth/",
access_key: "********************************",
secret_key: "********************************",
app_id: "http://localhost:8000/",
device_id: "developer"
};
let token;
function getToken() {
request(options, function(error, response, body) {
if (error) {
console.log(error);
} else {
console.log(body);
token = body.auth.access_token;
console.log(token);
// console.log(response);
}
});
}
module.exports = { getToken };
But i am not able to access the data from the api when i am using the nodejs code.
Thanks in advance
👨‍🏫 For an example, you can look at this code below: 👇
var options = {
method: "POST",
// hostname: "https://rest.cricketapi.com",
uri: "https://rest.cricketapi.com/rest/v2/auth/",
// make sure, on this API, use body or headers.
// If body, you can change this `headers` to `form`.
headers: {
access_key: "********************************",
secret_key: "********************************",
app_id: "http://localhost:8000/",
device_id: "developer"
}
};
let token;
function getToken() {
return new Promise((resolve, reject) => {
request(options, function(error, response, body) {
if (error) return reject(error)
body = JSON.parse(body);
console.log(body);
token = body.auth.access_token;
return resolve(token);
});
})
}
I hope it can help you 🙏.

Always get 502 error when calling AWS lambda endpoint from Node Js using `requestjs`

trying to post data in our AWS serverless api using Nodejs Request package but always get 502 error and can post data from the front end app (React or Jquery).
var dataToPost = {
name: 'ABC',
address: 'XYZ'
}
request(
{ method: 'POST'
, uri: 'url here...'
, headers: {
'User-Agent': 'request'
} , multipart:
[ { 'content-type': 'application/json'
, body: JSON.stringify(dataToPost)
}
]
}
, function (error, response, body) {
if(response.statusCode == 201){
console.log('document saved')
} else {
console.log('error: '+ response.statusCode)
console.log(body)
}
}
)```
If you are able to post data using react and Jquery then probably you are not making a post request correctly.Try this code for post request :
const request = require('request');
var dataToPost = {
name: 'ABC',
address: 'XYZ'
}
const options = {
url: 'url goes here',
json: true,
body: dataToPost
};
request.post(options, (err, res, body) => {
if (err) {
return console.log(err);
}
console.log(`Status: ${res.statusCode}`);
console.log(body);
});
Alternatively you can also use axios which makes code more readable and have inbuilt promise support:
const axios = require('axios');
var dataToPost = {
name: 'ABC',
address: 'XYZ'
}
const url = 'put url here'
axios.post(url, data)
.then((res) => {
console.log(`Status: ${res.status}`);
console.log('Body: ', res.data);
}).catch((err) => {
console.error(err);
});
Also check what does AWS Lmbada logs says.

Nodejs Post attachment to JIRA

I am receiving http POST response OK 200, but I see no file present on JIRA issue. From my research I can understand that it could be some problem with formData I am sending with request. Below is my code:
var newBuffer = new Buffer(req.Payload, 'base64');
var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({
frequency: 10, // in milliseconds.
chunkSize: 2048 // in bytes.
});
// With a buffer
myReadableStreamBuffer.put(newBuffer);
var formData = {
'file': {
'content': myReadableStreamBuffer,
'filename': req.FileName,
'mimeType': req.MimeType //mimeType from JSON
}
};
var options = {
url: 'https://comapny.atlassian.net/rest/api/2/issue/' + req.ReferenceId + '/attachments',
method: "POST",
json: true,
headers: {
'ContentType': 'multipart/form-data',
'Authorization': 'Basic ' + new Buffer(config.jira.jiraUser.userName + ':' + config.jira.jiraUser.password).toString('base64'),
'X-Atlassian-Token': 'nocheck'
},
formData: JSON.stringify(formData)
};
request(options,
function (error, response, body) {
if (error) {
errorlog.error(`Error Message : PostAttachmentToCSMS : ${error}`);
return response.statusCode;
}
else {
successlog.info(`Attachment posted for issue Key: ${req.ReferenceId} ${response.statusMessage}`);
return response.statusCode;
}
});
I can write file from myReadableStreamBuffer, so that seems ok. Please help me to identify the problem. Many thanks!
After spending some more time on it, I have found the correct format for formData:
var newBuffer = new Buffer(req.Payload, 'base64');
var formData = {
file: {
value: newBuffer,
options: {
filename: req.FileName,
contentType: req.MimeType
}
}
};
For whom like me getting errors with this API.
After struggling so many hrs on this thing, I finally found this works like a charm. I've got "XSRF check failed" 403/404 error message before writing this code.
// Don't change the structure of formData.
const formData = {
file: {
value: fs.createReadStream(filepath),
options: {
filename: filename,
contentType: "multipart/form-data"
}
}
};
const header = {
"Authentication": "Basic xxx",
// ** IMPORTANT **
// "Use of the 'nocheck' value for X-Atlassian-Token
// has been deprecated since rest 3.0.0.
// Please use a value of 'no-check' instead."
"X-Atlassian-Token": "no-check",
"Content-Type": "multipart/form-data"
}
const options = {
url: "http://[your_jira_server]/rest/api/2/issue/[issueId]/attachments",
headers: header,
method: "POST",
formData: formData
};
const req = request(options, function(err, httpResponse, body) {
whatever_you_want;
};
I was able to post attachments to JIRA using axios in the following way:
const axios = require('axios');
const FormData = require('form-data')
const fs = require('fs');
const url = 'http://[your_jira_server]/rest/api/2/issue/[issueId]/attachments';
let data = new FormData();
data.append('file', fs.createReadStream('put image path here'));
var config = {
method: 'post',
url: url,
headers: {
'X-Atlassian-Token': 'no-check',
'Authorization': 'Basic',
...data.getHeaders()
},
data: data,
auth: {
username: '',
password: ''
}
};
axios(config)
.then(function (response) {
res.send({
JSON.stringify(response.data, 0, 2)
});
})
.catch(function (error) {
console.log(error);
});

One Signal Push Notification

How to include small icon and big icon url while creating a push notification in node js, I know that we need to send image url while sending push notification, i need code sample in node js.. I have this code..
sendNotificationToSpecific =function (token,messages) {
var sendNotification = function (data) {
var headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Basic MDNlMTNjYWMTgy"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
var https = require('https');
var req = https.request(options, function (res) {
res.on('data', function (data) {
console.log("Response:");
console.log(JSON.parse(data));
});
});
req.on('error', function (e) {
console.log("ERROR:");
console.log(e);
});
req.write(JSON.stringify(data));
req.end();
};
var message = {
app_id: "awer342-d744-4787-b59a-f55c6215c491",
contents: {"en": messages},
include_player_ids: [token],
};
sendNotification(message);
};
As you can see in the docs https://documentation.onesignal.com/reference#section-appearance you can simply extend your message object like
var message = {
app_id: "xxxx",
contents: {"en": messages},
include_player_ids: [token],
small_icon: "resource_name", // can not be an url
large_icon: "http://url/ or resource_name"
}

Implementation of Box View API in Node.js gives 202

I'm currently building a node implementation of the new Box View API and I'm getting a 202 everytime I upload a document and retrieve a session. However, if I do a curl call, I dont get a 202. Is there anyone else experiencing this issue?
Here is my Ember Implementation:
export default Ember.View.extend({
document: null,
documentID: null,
session: null,
sessionID: null,
getDocument: function() {
var self = this;
return Ember.$.ajax({
url: 'http://localhost:3000/doc',
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({ "docURL": this.textField.value })
}).then(function(response){
self.set('document', response);
self.set('documentID', response.document_id);
});
},
getSession: function() {
var self = this;
return Ember.$.ajax({
url: 'http://localhost:3000/sess/',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ "docID": this.get('documentID') })
}).
then(function(response) {
self.set('session', response);
self.set('sessionID', response.session_id);
});
}.observes('documentID'),
actions: {
upload: function() {
this.getDocument();
}
}
});
Here is my node implementation:
var https = require('https');
var requestCount = 0;
exports.doc = function(req, res) {
var docURL = req.body.docURL;
var httpReq;
var opts = {
hostname: 'view-api.box.com',
path: '/1/documents',
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Token <my token>' }
};
res.header('Access-Control-Allow-Origin', '*');
httpReq = https.request(opts, function(preq, pres) {
var output = '';
preq.on('data', function(chunk) {
output += chunk;
});
preq.on('end', function() {
output = JSON.parse(output);
output.document_id = output.id;
delete output.id;
res.json(output);
});
});
httpReq.write(JSON.stringify({ "url": docURL }));
httpReq.end();
};
exports.sess = getSession;
function getSession(req, res) {
var docID = req.body.docID;
var httpReq;
var opts = {
hostname: 'view-api.box.com',
path: '/1/sessions',
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Token <my token>' }
};
res.header('Access-Control-Allow-Origin', '*');
httpReq = https.request(opts, function(preq, pres) {
var output = '';
if(preq.statusCode === 202) {
setTimeout(function() {
console.log('Retrying Request :: Count(' + requestCount + ')');
if (requestCount >= 3) {
res.json({ 'error': "Retry Again.", 'time': preq.headers['retry-after'] });
return;
}
getSession(req, res);
requestCount += 1;
}, 2000);
return;
}
preq.on('data', function(chunk) {
output += chunk;
});
preq.on('end', function() {
console.log('Successful Request!');
requestCount = 0;
output = JSON.parse(output);
output.session_id = output.id;
delete output.id;
res.json(output);
});
});
httpReq.write(JSON.stringify({ "document_id": docID, "duration": 60 }));
httpReq.end();
}
But now I'm getting this error. Is there a UI that can help me remove the uploaded documents?
{
"message": "You have exceeded your document upload rate-limit.",
"type": "error",
"request_id": "49f8b480b304496987b8cf21f5850c90"
}
You have the correct approach with retry-after for sessions.
The rate limiting you're seeing is actually due to the 2-document rate limit in place for the View API beta. See the FAQ for more info.
You can use webhooks to be notified when your documents finish converting (allowing you to upload another), so you don't have to poll the /documents endpoint for status.

Resources