AST
AST

Reputation: 48

How to call WCF rest service with credentials in node js?

I am calling WCF rest service inside nodejs API but it is failing with error 401: unauthorized. I know the credential provided by me is correct because when I call wcf rest service with same credential in SOAP UI it works.

Below is the NodeJs code I have.

var express =require('express');
var router=express.Router();
var http = require('http');

//format of actual user name
var username='domain\user_name';

//password has special characters like & ()
var password='';
var auth="Basic " + new Buffer(username + ":" + password).toString("base64");
//Rest service is using HTTPS
var options={
    hostname:'mycomputer.domain.com',
    method:'GET',
    path: '/myrest-service/v1/somedetails/somedata?param1=somevalue&param2=somevalue',    
    headers: {
        "Authorization": auth
    }
}

router.get('/',function(request,response){
    var result='';
    const req = http.request(options, (res) => {
        
        res.on('data', (chunk) => {
            console.log('chunk is received.');
            result=result + chunk;
        });
        res.on('end', () => {
          console.log('No more data in response.');          
          response.send({output: result});
        });
      });
      
      req.on('error', (e) => {
        console.error(`problem with request: ${e.message}`);
      });      
      
      req.end();
});

module.exports = router;

I also tried to provide options using below format

var options={
    hostname:'mycomputer.domain.com',
    method:'GET',
    path: '/myrest-service/v1/somedetails/somedata?param1=somevalue&param2=somevalue',    
    auth:auth
}

None of the option worked. Any suggestion?

Upvotes: 0

Views: 447

Answers (1)

Anil
Anil

Reputation: 1857

If user name has forward slash, then you need to provide user name like this:

var username='domain\\user_name';

Single "\" will be ignored. Instead use two "\".

Upvotes: 1

Related Questions