0

the idea is to connect to an external system from NS and get the token from https response. First of all, I converted the couple (login, password) to the base64,then I created a header contain the param of authorization in a specific format, and finally I send my https request . But the response code returned is 406 and not 200 :/ .

Below an exemple of the structure of my code

var stringInput = "email:password";
  
  var base64EncodedString = encode.convert({
   string: stringInput,
   inputEncoding: encode.Encoding.UTF_8,
   outputEncoding: encode.Encoding.BASE_64
  });

  var auth = 'Basic ' + base64EncodedString;
  var _headers = false;
  _headers = {
    Authorization: auth
  }; 
  var url_ = 'https:/..../api/auth/token';
    
   var  auth_resp = https.get({
    url: url_,
    headers: _headers
  });
    
    if (auth_resp.code == 200) {
   
  return  JSON.parse(auth_resp.body);
    }

Have you any idea about that! and Thanks a lot :D

spring_flower
  • 71
  • 1
  • 1
  • 9
  • I have done exactly what you are trying to do inside NetSuite without issue and my code looks almost identical to yours. I'm guessing the API you are requesting wants more headers in order to process your request, adding something like `'Accept': 'application/json'` to your header might do the trick. The API might have documentation on this somewhere as well. – Jon Lamb Jul 19 '18 at 18:38
  • Thanks @JonLamb for ur quik answer , but I tested that with Postman and it works without adding content-type or accept :/ , also I added the "content-type" inside my code and it doesn't work again :/ Thanks for ur answers :) and have a nice day :D – spring_flower Jul 20 '18 at 07:32
  • Hmm, that's curious. It seems to me that either Postman or NetSuite are adding extra headers to the request. Maybe you could try clicking the "code" button in Postman and copy the javascript>xhr code into the browser and run it, just to make sure it works in the browser and you'll be able to see all the headers that postman is adding. – Jon Lamb Jul 20 '18 at 13:42

2 Answers2

3

I have to post my solution for other NS developer, Thanks a lot for @JonLamb and @bknights for ur answers that helped me!

my code work now !

var stringInput = "email:password";
  
  var base64EncodedString = encode.convert({
   string: stringInput,
   inputEncoding: encode.Encoding.UTF_8,
   outputEncoding: encode.Encoding.BASE_64
  });

  var auth = 'Basic ' + base64EncodedString;
  var _headers = false;
  _headers = {
        'Accept': 'application/json',
    'authorization': auth
  }; 
  var url_ = 'https:/..../api/auth/token';
    
   var  auth_resp = https.get({
    url: url_,
    headers: _headers
  });
    
    if (auth_resp.code == 200) {
   
  return  JSON.parse(auth_resp.body);
    }
spring_flower
  • 71
  • 1
  • 1
  • 9
0

Please check out the meaning of the 406 error

And then consider the api docs of the system to which you are trying to connect. You may need an Accept header or possibly the receiving server is misconfigured and they don't accept GET requests and should have returned a 405 instead.

Finally I assume in your sample: https:/..../api/auth/token the .... is a replacement for the real host name. Otherwise your response would be some sort of relative response from Netsuite itself.

bknights
  • 14,408
  • 2
  • 18
  • 31
  • Thanks @bknights for ur answer , but I tested that with Postman with GET requests : and it works without adding content-type or accept :/ , also the documentation of the api need a Get method Thank u for ur answers :) and have a nice day :D – spring_flower Jul 20 '18 at 07:37