I am executing a restlet by a post request in a suitelet. The restlet runs fine and I am able to pass data back from restlet--->suitelet, but I am unable to pass data from the suitelet to the restlet when I call the restlet.
suitelet
var test = 'test';
var parameters = {'custscripttest_input':test};
var apiURL='https://3864948-
sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?
script=xxx&deploy=1';
try{
var restletResponse=https.post({
url:apiURL,
headers:header,
body:postData,
params:parameters
});
restlet
var testInput =
runtime.getCurrentScript().getParameter({name:'custscripttest_input'});
No parameters show in runtime.getCurrentScript. I have created the parameter in the restlet script record.
I have also tried passing body data and retrieving in the restlet like so:
var data = context.request.parameters
I seemed to have overlooked my body:postData format. My post data body structure in my suitelet was
var postData={input:{"cust_input": test}
I retrieved data in the restlet using context.input.cust_input;
The params in an http(s) request are added to the url. They are not passed through to the script deployment parameters of the restlet.
Restlets don't give access to query string parameters when a body is included.
If you need the parameters you could format your postbody as:
body:JSON.stringify({
params:parameters,
body:postData // have postData still as an object at this point.
})
Related
Here In my mongo collection I have few records. I am writing an API in node js where I pass the ID in request body. My API have to take ID from request body and need to fetch it's whole records in response. but I am getting empty array.
My mongo collection record:
[{ accountId:"a1",name:"test"}]
My Node js code approach:
exports.getById = async (req) =>{
let id = `"` + req.body.accountId +`"`;
let accountId = await db.collection('account-details').find({id}).toArray();
return accountId
}
In request body, I am passing accountId as :
{
"accountId":"a1"
}
The ID is in the form of string. When I am trying to fetch whole records with the provided ID through my API, I am getting response as []. Can anyone please help me on this
You cannot send a body with an HTTP GET, therefore it will not be available in the req object, so req.body.accountId will be undefined and id will be an empty string.
You need to use POST (or PUT) to send body data.
But the more usual solution in your situation is to use GET with dynamic routing, where you add the id to the route e.g.: http://example.com/api/accounts/1234
If you use Express, in your router you need to add the route api/accounts/:id to your router. The id will be available in your code as req.params.id.
See Express docs. For other frameworks this will be something alike.
Hope this helps.
Using this example RESTlet code below, is there any way to get information about the HTTP request's calling URL? I know my deployment URL is this (shown below), but how can I get that information in my get, delete, post, or put functions?
https://123123-abc.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1
I call the above URL in Postman or other client apps and I pass in header info and a JSON payload. I only have access to the JSON payload inside my RESTlet code below. I would like to get access to the URL that was called.
/**
*#NApiVersion 2.x
*#NScriptType Restlet
*/
define(['N/record', 'N/error'],
function(record, error) {
// Get a standard NetSuite record
function _get(context) {
// Is there a way to get information about the request URL at this point?
return JSON.stringify(record.load({
type: context.recordtype,
id: context.id
}));
}
return {
get: _get,
delete: _delete, // not shown
post: post, // not shown
put: put, // not shown
};
});
I am answering my own question. This is one way I found to get the URL navigation paths within Netsuite. Using the N/url module provided by Netsuite, I can do the following. If there are better ways, let me know.
require(['N/url'], function(url) {
var host = url.resolveDomain({
hostType: url.HostType.APPLICATION
});
console.log(host)
// result = 123123-abc.app.netsuite.com
})
[I am trying to send parameter with GET request on RESTLET Netsuite !!
but i am continuously facing issue 403 forbidden when i am trying to concatenate parameter with URL , without parameter Request is working good and getting response but i want to send parameter so now i am trying to send parameter through Header but not able to read on server side !! please help me out .how to read request header values/parameters on Netsuite RESTLET.
1-first i tried concatenate with +"&name=asd" and then Uri builder throws error 403 forbidden
2-Now i am sending parameters through request.header.add("name":"asd") working good
This is my client side request code in which i am attaching parameters in request header
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.ContentType = "application/json";
request.Method = "GET";
request.Headers.Add(header);
request.Headers.Add("email", "polutry#key-stone.co");
request.Headers.Add("password", "123-4567");
WebResponse response = request.GetResponse();///here i got 403 on concatination ////
function getCustomer(context)
{
var request =https.request.headers;///headers function not defined
// var email =request.email;
//var password = request.password;
}
Want to get header values on server side][1]
[1]: https://i.stack.imgur.com/SEB68.png
In RESTlet you don't have access to request-headers, instead you get all the arguments passed to RESTlet in scriptContext(function argument to RESTlet entry point, get in current case).
Check this out on how to use RESTlet and how you can pass arguments.
Note: Add { "Content-Type": "application/json" } header since you want to pass data to RESTlet.
Looking for some assistance on how to return a value from a Suitelet popup to a client script.
I have a client script triggered by a field change which executes the suitelet:
var url = nlapiResolveURL('SUITELET', 'customscriptnbi_weeklytimesheet_suitelet','customdeployso_lineselectionpopupsuitele',false);
var resp = window.open(url,'_blank','width=300,height=300,titlebar=0,status=no,menubar=no,resizable=0,scrollbars=0');
On the Suitelet, how can I "write" the response back to the client script:
if (request.getMethod() == 'GET' ) {
var form = nlapiCreateForm('Select Sales Order Line',true);
form.setScript('customscriptnbi_weeklytimesheet_slet_cs')
form.addSubmitButton('Set');
response.writePage(form);
} // END 'GET' METHOD
else {
response.write('<html><body><script>window.parent.close(); </script></body></html>');
} // END 'POST' METHOD
any assistance would be much appreciated.
Looks like you are using a client script on your Suitelet. You can put a custom field on the record where the client script that calls the Suitelet is deployed at.
Then in you client script that is attached to the Suitelet, you can do window.opener.nlapiSetFieldValue(,);
I have to send array of ids in GET request as paramenter.How can I test it in Postman(google chrome extension for API testing)?
The scenario is I have url, www.something.com/activity/poi_ids
poi_ids should conatain arrays of ids like [316,318]
At api side using express,
app.get('/activity/:poi_ids',function(req,res){
var poi_ids = req.params.poi_ids;
.......
.......
});
I have looked into it but it is only for post request
you can send them via query params ..
in your http query params assign all values to same variables like
GET activity?pid=12&pid=23&pid=34
and then inside your express get it like
var ids=req.query.pid; //[12,23,34]
It is unstructured text. If you want to "send an array" then you'll need to design some way to encode it and then write JavaScript to decode it.
For example:
GET /activity/316-318
and
var poi_ids = req.params.poi_ids.split("-");