Has anyone been able to communicate with RESTlets ...
# suitescript
t
Has anyone been able to communicate with RESTlets using the fetch library? I am specifically using fetch in a React application
b
ive used note-fetch serverside, but i wouldnt expect it to be reasonable to put credentials clientside
t
Here is my fetch code. Do you see anything wrong with it? fetch('https://5557777-sb2.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=5555&deploy=2', { method: 'GET', mode: 'no-cors', headers: { 'Authorization': 'NLAuth nlauth_account=account, nlauth_email=email, nlauth_signature=password, nlauth_role=3', 'Content-Type': 'application/json', 'User-Agent-x': 'SuiteScript_Call' } }) .then(response => {return response.json()}) .catch(error=>console.log(error))
b
im assuming you are getting cors errors
i would only expect clientside access to a restlet to work from internal urls from netsuite
t
The react app is rendered by a Suitelet. So I thought that could work because I am using the React framework kinda like a client script. So you're saying there is no way to get past the cors errors?
b
if you send the request from an internal page (ie you have the jsessionid cookie)
then you should send the get to the internal url
t
Sorry, I guess I don't understand how this can be done with React.
Or do I get this cookie from my Suitelet?
b
whats the url of your suitelet
im specifically looking to see if its an internal url or external url
b
thats an internal url
that means the user must be logged into netsuite, which means the browser sending the fetch has access to other internal urls
use the internal url of the suitelet (dont use no-cors mode) for your fetch and the request will include the necessary headers to access the internal url
t
So I cannot use a RESTlet url?
b
your restlet has 2 urls
use the internal one
t
So I got a 403 error. Here is my code. fetch('https://555777-sb2.app.netsuite.com/app/site/hosting/restlet.nl?script=5577&deploy=2', { method: 'GET', headers: { 'Authorization': 'NLAuth nlauth_account=account, nlauth_email=email, nlauth_signature=password, nlauth_role=3', 'Content-Type': 'application/json', 'User-Agent-x': 'SuiteScript_Call' } }) .then(response => {return response.json()}) .catch(error=>console.log(error))
b
Dont send authorization header
❤️ 1
t
It worked! OMG you're the best