I'm getting this error "ReferenceError: responceBody is not defined" - postman

i have written my test script code like this
var responce=JSON.parse(responceBody);
tests["scope"] = responce.scope == "APP";

There's a typo in your responseBody variable, try: JSON.parse(responseBody)

Related

Cannot set global variable in postman script

When I log in to a Fortinet device via API, It returns a variable called fpc-sid. This is their version of an authtoken.
When I attempt to put this on a global variable on the test section in the login request it returns the fpc-sid and then I get:
"ReferenceError: sid is not defined"
For some reason, I think it doesn't like the "-", but not sure.
How can I set fps-sid as a global variable?
var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("fpc-sid", jsonData.fpc-sid);
var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("fpc-sid", jsonData.fpc-sid);
Property cannot be accessed like that when it is not a valid identifier. use it by name as :
var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("fpc-sid", jsonData['fpc-sid']);
Read: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors
Also use the pm object pm.globals.set() for setting variable as Danny mentioned. This is the new syntax, this will make it more future proof

How to replace text in message with regex with variable get in sheet in google apps script

Firstly, I'm using google apps script
I get an text body and I want to replace placeholder with variables in a sheet, I get a variable in my sheet and i want to replace it with regex but it's not working
it's working with a variable that i just set but not with an value that i get in a sheet... I don't know why...
function replaceInBody() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var lastLigne = sheet.getLastRow()
var lastColumn = sheet.getLastColumn()
var firstLigne = sheet.getRange(1,1,1,lastColumn).getValues();
var newBody
Logger.log('firstLigne: %s', firstLigne)
var data
var bodyTest = 'blablabla {name} blablabla {var1} blabla'
Logger.log('bodyTest: %s', bodyTest)
var notwork = firstLigne[0][2]
var work = 'name'
Logger.log('notwork: %s', notwork) // finally it's work by rewriter the code
Logger.log('work: %s', work)
Logger.log('**')
Logger.log(new RegExp("{"+ work +"}", 'g'))
newBody = bodyTest.replace(new RegExp("{"+ notwork +"}", 'g'), 'changed')
Logger.log('newBody: %s', newBody)
newBody = bodyTest.replace(new RegExp("{"+ work +"}", 'g'), 'changed')
Logger.log('newBody: %s', newBody)
}
```
just my text is not change with notwork variable but it's the same variable... // finally it's work by rewriter the code but I don't know my first mistake thanks to all of you :p
You need to change your replace() method to:
var regex1 = 'blablabla {name} blablabla {name} blabla';
console.log(regex1.replace(/name/g, 'Changed'));
This will find every appearance of "name" and change it. You can take a look at the method's documentation to see how to work with these more simply.

How to extract the values from the response body in postman

After posting the request, API return response body as string
Response body look like
{ UniqueID = 93243434,birthGender = M,birthDate = 11/1/2018 5:51:18
PM, familyNames = James, givenNames = Test }
when I try to set the environment variable using the below code
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable("currentUniqueId", data.UniqueId);
I got the below error on test results
Error message:
There was an error in evaluating the test script: JSONError:
Unexpected token 'U' at 1:3 { UniqueID = 93243434,birthGender =
M,birthDate = 11/1/2018 5:51:18 PM, family ^
my goal is I need to extract the value 93243434 and assign to environment variable.
Hi you are using the correct way but you can try this version
var jsonData = pm.response.json();
pm.environment.set("UNIQUE_ID", jsonData.UniqueID);
The set("UNIQUE_ID" will help you save it in variable and you can name it as you want and jsonData.uniqueID will extract what you want to get from the Json response
If you view my approach I am extracting Access code and company id and saving it in variable and calling it in all next api's
You are using a notation pattern that is deprecated.
Instead of set your variable using:
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable("currentUniqueId", data.UniqueId);
Try to set your variable this way:
pm.environment.set('currentUniqueId', pm.response.json().UniqueID);
To get more information, try: https://learning.getpostman.com/docs/postman/scripts/test_examples/

Postman - How can I compare a message using vars

can anyone help me, I am almost new in Postman.
my issue is as follow:
I send a POST request and get a message asresponse:
{
"errorCode": 1000,
"errorDescription": "Account is not verified by Admin!"
}
this message is already saved in a var named "messageAccountIsnotVerified"
when I try to use comparison in postman tets script and compare the message with the expected string is working fine:
pm.test("test", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.errorCode).to.eql(1000);
pm.expect(jsonData.errorDescription).to.eql("Account is not verified by Admin!");
});
But When I try to save the String text "Account is not verified by Admin!" in a variable named: messageAccountIsnotVerified
and try to make the same comparison
pm.test("test", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.errorCode).to.eql(1000);
pm.expect(jsonData.errorDescription).to.eql("messageAccountIsnotVerified");
});
or
pm.test("test", function () {
var jsonData = pm.response.json();
var message = pm.environment.get("messageAccountIsnotVerified");
pm.expect(jsonData.errorCode).to.eql(1000);
pm.expect(jsonData.errorDescription).to.eql(message);
});
it failed with error:
test | AssertionError: expected 'Account is not verified by Admin!' to deeply equal 'messageAccountIsnotVerified'
Can someone explain to me
1. what does the "deeply equal" mean and
2. what do I wrong and
3. how can I use the assertion by using the variable
Thanks for any Hint
Just additional Info: I have the same issue when I compare email with
# sign in another message - so I assume may be something to do with special chars
You need to get the variable in the following way:
pm.expect(jsonData.errorDescription).to.deep.equal(pm.environment.get("messageAccountIsnotVerified"))
.to.deep.equal can be used to reference something further down in a nested object.
Add .deep earlier in the chain to use deep equality instead. See the deep-eql project page for info on the deep equality algorithm: https://github.com/chaijs/deep-eql.
Looking at the response body you posted { "errorCode": 1000, "errorDescription": "Account is not verified by Admin!" }, I don't see a problem with the first test but for it to complain about deep equal then this response is probably no exactly as you posted.
You could reduce this all down again if you wanted too:
pm.test("test", () => {
pm.expect(pm.response.json()).to.deep.equal({"errorCode": 1000, "errorDescription": "Account is not verified by Admin!"})
});
That test would do the same as above.
let string = pm.response.json()
pm.expect(string).to.equal("String from Response")

Remove text qualifier when copying to variable in postman

I have an issue using a text variable from a response body and inserting into a request without the text qualifiers.
I'm trying this:
var data = JSON.parse(responseBody);
postman.setGlobalVariable("basketid", responseBody);
This is the response
"14b5f921-78d9-4ab2-a5a0-828f00fcf63a"
When I look at the basketid variable the text qualifiers are still there which mean that when I call
{{url}}/api/{{basketid}}
I get an error.
Do anyone know of a way to save the variable without text qualifier?
The following worked for me:
var _token = responseBody.slice(1,-1);
pm.globals.set("token", _token);
If you are getting "14b5f921-78d9-4ab2-a5a0-828f00fcf63a" as it is in global environment as you said, you can use eval:
var jsonObj = JSON.stringify(responseBody);
var setObj=eval("("+jsonObj+")");
postman.setGlobalVariable("basketid",setObj);
I ran into the same issue today while trying to store my token and this is what worked for me:
var data = JSON.parse(responseBody);
postman.setGlobalVariable("token", data.token);