Thursday, November 15, 2018

Call a RESTlet from a Client script

 

As stated in the Help Guide, authentication is required for RESTlets.  However, in cases where a RESTlet is called from a client hosted by NetSuite, the authentication headers are no longer required.  When calling a RESTlet from a Client script, the following code may serve as a guide:

var url ='/app/site/hosting/restlet.nl?script=380&deploy=1';
var headers = new Array();
headers['Content-type'] = 'application/json';
var result = nlapiRequestURL(url, null, headers);
alert(results.getBody());

Take note, however, that the code above only works for Client scripts.  When invoking RESTlets from server scripts such as Suitelets, User Events, and Scheduled scripts, the authentication headers are still required, as follows:

var url = 'https://rest.netsuite.com/app/site/hosting/restlet.nl?script=380&deploy=1';
var headers = new Array();
headers['Content-type'] = 'application/json';
headers['Authorization'] = 'NLAuth nlauth_email=email@domain.com, nlauth_signature=password, nlauth_account=123456, nlauth_role=3';
var result = nlapiRequestURL(url, null, headers);

Also, kindly take note of the difference between the RESTlet URLs.  Client scripts can use the internal URL of the RESTlet since authentication is handled by NetSuite, while for server scripts, the external URL needs to be used.

2 comments:

  1. You need an example that includes passing POST data to the RESTlet. I can't seem to figure out how to do that.

    ReplyDelete
  2. NLAuth was deprecated

    ReplyDelete