Reputation: 13892
I'm trying to write a node.js script that uses a Dynamics NAV Odata feed.
I have both a UserAccount/PW and a Web Services Access Key from my Dynamics NAV setup.
I can't for the life of my find out how to properly authenticate, either by adding something in a header or by adding something in the URL query. I've tried using the 'username:password@server' format. I've tried encoding that as base64 and adding that in the Header for the 'Authentication' value.
The documentation itself is incredibly non-specific. I know how to generate the key, but I don't know how to properly send that key to NAV to authenticate.
I'm using the 'request-promise' npm package, which takes an 'options' argument that I can add arbitrary header key:value pairs into. Please someone give me some direction about how to authenticate to NAV. I've been on this for hours.
Upvotes: 6
Views: 1651
Reputation: 13892
I found a satisfactory answer.
Using node-libcurl I was able to cURL to a url using the format
http://username:password@<server>/ODATA_table
specifically my cURL module looks like this:
var Curl = require('node-libcurl').Curl;
var curl = new Curl(),
close = curl.close.bind(curl);
function getOData(url) {
return new Promise((resolve, reject) => {
curl.setOpt(Curl.option.URL, url);
curl.setOpt(Curl.option.HTTPAUTH, Curl.auth.NTLM);
curl.setOpt(Curl.option.SSL_VERIFYPEER, false);
curl.setOpt(Curl.option.SSL_VERIFYHOST, false);
curl.setOpt(Curl.option.POST, 0);
curl.on('end', function (statusCode, body, headers) {
var retObj = JSON.parse(body);
resolve(retObj);
close();
});
curl.on( 'error', function(e){
reject(e);
close();
});
curl.perform();
})
}
module.exports = {getOData: getOData};
But I have to explicitly ask for json in the url, like ?format=json
.
Upvotes: 2
Reputation: 214
Tkol, you're right,also you can use guzzle, it's very simple, that's a sample function that query customers table :
public function ReadCustomer($identifier=0)
{
try {
$client = new GuzzleHttpClient();
$apiRequest = $client->request('GET', 'http://server:port/ServiceName/WS/CompanyName/Page/Customer?$filter=No eq \''.$identifier.'\'',[
'auth' =>'username','password', 'NTLM' ], //NTLM authentication required
'debug' => true //If needed to debug
]);
$content = json_decode($apiRequest->getBody()->getContents());
return $content;
} catch (RequestException $re) {
//For handling exception
}
}
you can check my sample: update/delete/get from Dynamics NAV OData webservice
Upvotes: -1