NODEJS SOAP: Object reference not set to an instance of an object - node.js

I got an error:
Object reference not set to an instance of an object
I'm missing something here in the request, but couldn't figure it out. Any help is appreciated.
I tried using soap module as well as strong-soap module, but the same error occurs in both. So it might be the error in request arguments.
Object reference not set to an instance of an object
Code:
"use strict";
var soap = require('strong-soap').soap;
var url = 'http://test.eprabhu.com/Api/Utility.svc?wsdl&UserName=CLIENT';
var requestArgs = {
'UserName': 'CLIENT',
'Password': 'CLIENT12',
'OperatorCode': 2,
'MobileNumber': '9803111111',
'Amount': 100,
'PartnerTxnId': 'P201904220218335187'
};
var options = {
'user-agent': 'sampleTest',
'Content-Type': 'text/xml;charset=UTF-8',
// 'soapAction': 'http://test.eprabhu.com/Api/Utility.svc?wsdl#MobileTopup',
'soapAction': 'http://tempuri.org/IUtility/MobileTopup'
};
soap.createClient(url, options, function(err, client) {
var method = client['MobileTopup'];
method(requestArgs, function(err, result, envelope, soapHeader) {
//response envelope
console.log('Response Envelope: \n' + envelope);
//'result' is the response body
console.log('Result: \n' + JSON.stringify(result));
console.log('Soap Header: \n', soapHeader);
});
});
Any help will be appreciated. Thanks

Change your args to:
var requestArgs = {
MobileTopupRequest: {
UserName: 'CLIENT',
Password: 'CLIENT12',
OperatorCode: 2,
MobileNumber: '9803111111',
Amount: 1,
PartnerTxnId: 'P201904220218335187'
}
};

Related

Authentication in Node JS soap request

I am using strong-soap (https://www.npmjs.com/package/strong-soap) for consuming wsdl from Node JS
I have a wsdl with header like below:-
<soapenv:Header>
<wsse:Security xmlns:wsse="http://xyz.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-24" xmlns:wsu="http://secure.xsd">
<wsse:Username>userid</wsse:Username>
<wsse:Password Type="http://pwdtext">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
I need to add this header information while creating client.
I tried like
var url = "test?wsdl";
soap.createClient(url, {wsdl_headers: {"Username": "username","Password":"password"} }, function(err, client) {
//some logic
});
But every time I was getting soap fault "Authentication Failed".
Any idea what I am doing wrong?
Thanks in advance.
I had the same issue trying to pull data.
my mistake was that in the options of the createClient method is was using headers instead of wsdl_headers
also set the same authentication on the client before any method is called
my code looks as
var url = 'https://datahere?wsdl';
var httpOptions = {
wsdl_headers: {
'Authorization': 'Basic ' + new Buffer('username' + ':' + 'password').toString('base64')
}
};
soap.createClient(url, httpOptions, function(err, client) {
if (err) {
console.log(err.message);
response.status(401).end();
} else {
var requestArgs = {
Method1: 'dummyData',
Method2: ''
};
// client.setSecurity(new soap.BasicAuthSecurity('password', 'password'));
client.addHttpHeader('customHeader1', 'words');
client.addHttpHeader('Authorization', "Basic " + new Buffer('username-app-maker' + ':' + 'password').toString('base64'));
client.GETSOAPMETHOD(requestArgs, function(err, result) {
if (err) {
console.log(err.message);
}
console.log('i found ' + result);
response.send(result);
});
}
});
As mentioned in this answer, wsdl_header object is expecting a key 'Authentication'
So try running the following code:
var url = 'test?wsdl';
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");
soap.createClient(url, { wsdl_headers: {Authorization: auth} }, function(err, client) {
});

New to Typescript & Angular problems with Http Post

I'm new to Angular and Typescript and I try to create a simple login page. I've created a service in typescript which is invoked when the user presses the 'Login' button. The textboxes which contains the username and password is bound correctly to the model, but when I send the request to my backend written in C#, it will not hit the breakpoint which I suspect is because of the format of the message being sent on the wire.
So using PostMan, I'm able to invoke the service and get back an access_token When exporting the request to code in PostMan this is what the NodeJS variant look like:
var request = require("request");
var options = { method: 'POST',
url: 'http://localhost:8081/login',
headers:
{ 'postman-token': '34dd4d0f-ff16-db4f-ebae-dab945729410',
'cache-control': 'no-cache',
'content-type': 'application/x-www-form-urlencoded' },
form: { username: 'test', password: 'test', grant_type: 'password' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
And this is my Typescript code
login(userName: string, password:string) : Observable<boolean> {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded')
var content = JSON.stringify({
username: userName,
password: password,
grant_type: this.grant_type
});
return this.http.post(this.authenticationEndpoint, content, {headers: headers})
.map((response:Response) => {
let token = response.json() && response.json().token;
if(token){
//this.token = token;
localStorage.setItem('user', JSON.stringify({userName: userName, token:token}));
return true;
}
return false;
});
}
This results in an error in Visual Studio Code, which says:
I'm not really sure how I should interpret this error message, but since the method in my webservice is not invoked I'm pretty sure that it has something to do with the HTTP headers or the format of the Http Post.. Any ideas?
Using URLSearchParams as the body of the request and angular will automatically set the content type to application/x-www-form-urlencoded
import { URLSearchParams } from "#angular/http"
let body = new URLSearchParams();
body.set('username', username);
body.set('password', password);
.....
this.http.post(this.authenticationEndpoint, body).map(..)

How to upload assets to a github release from node.js

I am trying to automatically post some assets on a Github release I programmatically create.
Here is my upload function:
function uploadFile(fileName, uploadUrl, callback){
var uploadEndPoint = url.parse(uploadUrl.substring(0,uploadUrl.indexOf('{')));
options.host = uploadEndPoint.hostname;
options.path = uploadEndPoint.pathname+'?name=' + fileName;
options.method = 'POST';
options.headers['content-type'] = 'application/zip';
var uploadRequest = https.request(options, callback);
uploadRequest.on('error', function(e) {
console.log('release.js - problem with uploadRequest request: ' + e.message);
});
var readStream = fs.ReadStream(path.resolve(__dirname,'builds',fileName));
readStream.pipe(uploadRequest);
readStream.on('close', function () {
req.end();
console.log('release.js - ' + fileName + ' sent to the server');
});
}
At the end of this I get a 404 not found
I tried auth from token and user/password
I checked the url
I though it might be because of SNI, but I don't know how to check that.
Any clue ? Thanks !
I found a solution to that problem by NOT using the low level node.js modules and using instead restler which is a library that handles SNI.
Here is how is used it:
rest = require('restler'),
path = require('path'),
fs = require('fs');
fs.stat(path.resolve(__dirname,'builds',fileName), function(err, stats){
rest.post(uploadEndPoint.href+'?name=' + fileName, {
multipart: true,
username: GITHUB_OAUTH_TOKEN,
password: '',
data: rest.file(path.resolve(__dirname,'builds',fileName), null, stats.size, null, 'application/zip')
}).on('complete', callback);
});
Hope that will help someone :)
EDIT on 27/02/2015: We recently switched from restler to request.
var
request = require('request'),
fs = require('fs');
var stats = fs.statSync(filePath);
var options = {
url: upload_url.replace('{?name}', ''),
port: 443,
auth: {
pass: 'x-oauth-basic',
user: GITHUB_OAUTH_TOKEN
},
json:true,
headers: {
'User-Agent': 'Release-Agent',
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/zip',
'Content-Length': stats.size
},
qs: {
name: assetName
}
};
// Better as a stream
fs.createReadStream(filePath).pipe(request.post(options, function(err, res){
// Do whatever you will like with the result
}));
The upload_uri can be retrieved through a get request on an existing release or in the response directly after the release creation.

nodejs app - download file from sharepoint with NTLM AUTH

I must confess I'm stuck. I need a nodejs app to download a file from a SharePoint library. Simple, huh? No. Not the simple OOTB SharePoint. The only-ssl allowed, with specific mandatory header added and surely only domain-based NTLM authentication method.
I've tried httpntlm (https://www.npmjs.org/package/httpntlm) that seemed to just might work in advance, but no. The SP responses with something went wrong message.
I've tried node-sharepoint, but it doesn't support NTLM yet. The app gets ETIMEDOUT response.
Any ideas, please welcome.
I am able to download the file using httpntlm module.you need to change the few lines of code.Replace the waterfall logic with below code in httpntlm.js.
async.waterfall([
function ($){
var type1msg = ntlm.createType1Message(options);
httpreq.get(options.url, {
headers:{
'Connection' : 'keep-alive',
'Authorization': type1msg
},
agent: keepaliveAgent
}, $);
},
function (res, $){
if(!res.headers['www-authenticate'])
return $(new Error('www-authenticate not found on response of second request'));
var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
var type3msg = ntlm.createType3Message(type2msg, options);
if(method!=='download'){
httpreq[method](options.url, {
headers:{
'Connection' : 'Close',
'Authorization': type3msg
},
allowRedirects: false,
agent: keepaliveAgent
}, $);
}else{
//By Dheeraj for file download
httpreq.download(
url,
{
headers:{
'Connection' : 'Close',
'Authorization': type3msg
},
//allowRedirects: false,
agent: keepaliveAgent
},
__dirname + 'your_filename',
function (err, progress){
if (err) return console.log(err);
console.log(progress);
}, function (err, res){
if (err) return console.log(err);
console.log(res);
});
}
}
], callback);
};
['get', 'put', 'post', 'delete', 'head','download'].forEach(function(method){
exports[method] = exports.method.bind(exports, method);
});
and replace download method of httpreq.js(httpntm_module/node_modules/httpreq_module/httpreq.js) You can find it at Line number 79 approx.
exports.download = function (url,options, downloadlocation, progressCallback, callback) {
//var options = {};
options.url = url;
options.method = 'GET';
options.downloadlocation = downloadlocation;
options.allowRedirects = true;
// if only 3 args are provided, so no progressCallback
if(callback === undefined && progressCallback && typeof(progressCallback)==="function")
callback = progressCallback;
else
options.progressCallback = progressCallback;
doRequest(options, callback);
}
Please let me know if you are still getting issues.

Podio API addItem call

I'm trying to implement https://developers.podio.com/doc/items/add-new-item-22362 Podio API addItem call in a nodejs module. Here is the code:
var _makeRequest = function(type, url, params, cb) {
var headers = {};
if(_isAuthenticated) {
headers.Authorization = 'OAuth2 ' + _access_token ;
}
console.log(url,params);
_request({method: type, url: url, json: true, form: params, headers: headers},function (error, response, body) {
if(!error && response.statusCode == 200) {
cb.call(this,body);
} else {
console.log('Error occured while launching a request to Podio: ' + error + '; body: ' + JSON.stringify (body));
}
});
}
exports.addItem = function(app_id, field_values, cb) {
_makeRequest('POST', _baseUrl + "/item/app/" + app_id + '/',{fields: {'title': 'fgdsfgdsf'}},function(response) {
cb.call(this,response);
});
It returns the following error:
{"error_propagate":false,"error_parameters":{},"error_detail":null,"error_description":"No matching operation could be found. No body was given.","error":"not_found"}
Only "title" attribute is required in the app - I checked that in Podio GUI. I also tried to remove trailing slash from the url where I post to, then a similar error occurs, but with the URL not found message in the error description.
I'm going to setup a proxy to catch a raw request, but maybe someone just sees the error in the code?
Any help is appreciated.
Nevermind on this, I found a solution. The thing is that addItem call was my first "real"-API method implementation with JSON parameters in the body. The former calls were authentication and getApp which is GET and doesn't have any parameters.
The problem is that Podio supports POST key-value pairs for authentication, but doesn't support this for all the calls, and I was trying to utilize single _makeRequest() method for all the calls, both auth and real-API ones.
Looks like I need to implement one for auth and one for all API calls.
Anyway, if someone needs a working proof of concept for addItem call on node, here it is (assuming you've got an auth token beforehand)
_request({method: 'POST', url: "https://api.podio.com/item/app/" + app_id + '/', headers: headers, body: JSON.stringify({fields: {'title': 'gdfgdsfgds'}})},function(error, response, body) {
console.log(body);
});
You should set content-type to application/json
send the body as stringfied json.
const getHeaders = async () => {
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
};
const token = "YOUR APP TOKEN HERE";
headers.Authorization = `Bearer ${token}`;
return headers;
}
const createItem = async (data) => {
const uri = `https://api.podio.com/item/app/${APP_ID}/`;
const payload = {
fields: {
[data.FIELD_ID]: [data.FIELD_VALUE],
},
};
const response = await fetch(uri, {
method: 'POST',
headers: await getHeaders(),
body: JSON.stringify(payload),
});
const newItem = await response.json();
return newItem;
}

Resources