NetSuite SuiteScript 2.0 How to parse content Text in suitescript - netsuite

i am trying to update a vendor record status field using suitescript 2.0,passing the body in postman tool and is working fine for content JSON but the problem is when i try content as Text its getting error don't know how to read body value in suitescript 2.0.
input body from postman
sample code is
function doPut(context)
{
var obj=JSON.stringify(context.ids);-----here is the error context is empty
// tried JSON.parse also getting undefined
log.debug('str: '+obj);
return obj;
}

If you could provide the exact error message that would be helpful.
But in the mean time a few things that you should verify in your script, if you do not pass application/JSON in header, and your data is object, you need to explicitly parse it into JSON(i.e use JSON.parse() on the request-body), and your response type too should be in the same format i.e your response type should match content-type in the request.

looks like you may have the wrong Content-type. Should be Application-json. If not try Json.parse on the body if you're using text/plain. First step is to always log the context to console or run Object.keys(context) to see what's there. Also make sure doPut is exported as a function

Related

How to pass a variable to the body (not header/params) of a sampler

I have a request from where I am getting a token in response. I have captured that token into a variable using Regular Expression Extractor-Post processor.
Now, I want to pass this token to the body of the next sampler in two places, one as the contributionID and the second as the contrib_(pass the value).mp4
This is how the body looks:
enter image description here
The problem is, passing it via header manager is not working. And passing variable in body causes syntax error.
It seems that I would need to pass it using beanshell script or something else which I am unable to implement.
Any leads?
I have tried adding beanshell preprocessor in the sampler where I want to pass the value but I am unable to capture the body and the respective parameter.
It would be a huge help if I could get a hint towards a working solution.

Body is not getting parsed in GET method

I am using mockoon for API simulation. I created 2 routes there with method GET and its body contains(responds with) JSON object. I noticed that my express app is not able to parse one of the routes. But the route that has JSON object in body which contains ARRAY is getting parsed. I tested both routes with Express(by console.log) and in chrome browser(I have JSON formatter extension) and it is behaving the same meaning response that does not contain ARRAY is not getting parsed but the response with array is getting parsed(behaving normally). Let me show the screenshots:
Express(by console.log):
With array:
Without array:
Chrome(JSON Formatter extension):
With array(extension is able to parse):
Without array(extension is not able to parse):
I tried adding Header(Content-Type: application/json) to the route in mockoon. But still, I am not aware of what is going on here. Someone please explain
The express code:
const iabs_client = await axios.get(
"http://localhost:3001/iabs-client
);
Here is the route created in Mockoon(without array inside JSON):
P.S mockoon is a program that creates endpoints in localhost, useful for API simulation when developing front-end without having backend yet
The trailing comma after "something" is not valid JSON. Edit your Mockoon body to remove the comma and it should work.

Nodejs http server json parse

why the parameter data different in console and website
i need use JSON.stringify or JSON.parse in res.end?
The JSON.stringify() method converts a JavaScript object or value to a JSON string, so add this to your res.end()
res.end(JSON.stringify(data));
I think this code will help you.
res.end(data.toString())
I'll explain the reason why it is different in console and website.
In console, the data is type of buffer, so it can probably output data in byte format.
But in the web browser, the data is parsed in JSON string, so it'll be converted to string and correct result comes out.

Obtain data from JSON object with NODE JS

I have a response from a webservice like this:
[{"record_id":"63","date":"2021-04-12","acept":"1","name":"John","document":"1","passport":"","phone":"999999999","sign":"[document]","activity":"2"}]
There is a var called response that stores that response.
How do I get the "name" and the "phone" from this?
I tried to do JSON.stringify(response) in order to get somehow the info but I don't know what to do next.
Is the response a JSON or just a String??
Should I do JSON.stringify or JSON.parse to be able to work with this?
Thanks a lot
To check to see whether this is a JSON object or string, run console.log(typeof response). If it logs object, then this is already a JSON object! You don't have to do anything, and can get attributes out of it like any other object. (For instance, to get the name attribute, you can run response[0]["name"].) If it logs string, then you'll have to run JSON.parse(response) and save that to a variable to parse the string and turn it into an object.
After parse the response, you can access to name and phone like this:
response[0]["name"]
response[0]["phone"]

Azure Logic App: how to make a x-www-form-encoded?

I'm trying to make a request with Content-Type x-www-form-urlencoded that works perfectly in postman but does not work in Azure Logic App I receive a Bad Request response for missing parameters, like I'd not send enything.
I'm using the Http action.
The body value is param1=value1&param2=value2, but I tried other formats.
HTTP Method: POST
URI : https://xxx/oauth2/token
In Headers section, add the below content-type:
Content-Type: application/x-www-form-urlencoded
And in the Body, add:
grant_type=xxx&client_id=xxx&resource=xxx&client_secret=xxx
Try out the below solution . Its working for me .
concat(
'grant_type=',encodeUriComponent('authorization_code'),
'&client_id=',encodeUriComponent('xxx'),
'&client_secret=',encodeUriComponent('xxx'),
'&redirect_uri=',encodeUriComponent('xxx'),
'&scope=',encodeUriComponent('xxx'),
'&code=',encodeUriComponent(triggerOutputs()['relativePathParameters']['code'])).
Here code is dynamic parameter coming from the previous flow's query parameter.
NOTE : **Do not forget to specify in header as Content-Type ->>>> application/x-www-form-urlencoded**
Answering this one, as I needed to make a call like this myself, today.
As Assaf mentions above, the request indeed has to be urlEncoded and a lot of times you want to compose the actual message payload.
Also, make sure to add the Content-Type header in the HTTP action with value application/x-www-form-urlencoded
therefore, you can use the following code to combine variables that get urlEncoded:
concat('token=', **encodeUriComponent**(body('ApplicationToken')?['value']),'&user=', **encodeUriComponent**(body('UserToken')?['value']),'&title=Stock+Order+Status+Changed&message=to+do')
When using the concat function (in composing), the curly braces are not needed.
First of all the body needs to be:
{ param1=value1&param2=value2 }
(i.e. surround with {})
That said, value1 and value2 should be url encoded. If they are a simple string (e..g a_b) then this would be find as is but if it is for exmaple https://a.b it should be converted to https%3A%2F%2Fa.b
The easiest way I found to do this is to use https://www.urlencoder.org/ to convert it. convert each param separately and put the converted value instead of the original one.
Here is the screenshot from the solution that works for me, I hope it will be helpful. This is example with Microsoft Graph API but will work with any other scenario:

Resources