pm.request.url.toString() with value of env var instead of the placeholder - postman-pre-request-script

If request is using an environment variable then when using pm.request.url.toString() inside a Pre-requisite Script it outputs the query with the placeholder and not it's actual value.
meaning it will output https://somesite.com/api/v3/{{env_variable}} instead of https://somesite.com/api/v3/liststuff or whatever.
Is there anyway to get the URL with the value and not the placeholder?

Request is not resolved in the pre-request script since the variables could be further modified there.
But you can use the postman-collection library within the scripts to resolve the request yourself.
Thanks harryi3t for posting this script on GitHub
https://github.com/postmanlabs/postman-app-support/issues/3322
Here's a sample script
let sdk = require('postman-collection'),
newRequest = new sdk.Request(pm.request.toJSON()),
resolvedRequest = newRequest.toObjectResolved(null, [pm.variables.toObject()], { ignoreOwnVariables: true });
// prints the resolved request to console. Please check DevTools to see the structure
console.log({ resolvedRequest });
Kindly refer screenshot for the same in which custId takes value from placeholder and resolved in pre request also before excuting actual request

Related

PowerBI Failed to execute 'atob' on 'Window' in parsePowerBIAccessToken

Randomly today my powerbi embedded code has been throwing:
DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
at window.atob (eval at <anonymous> (https://app.powerbi.com/13.0.11674.244/scripts/reportembed.externals.bundle.min.js:1326:504), <anonymous>:1:83)
at e.parsePowerBIAccessToken (https://app.powerbi.com/13.0.11674.244/scripts/reportEmbed.min.js:1:2331307)
at e.isTokenTenantValid (https://app.powerbi.com/13.0.11674.244/scripts/reportEmbed.min.js:1:2331046)
at t.isPowerBIAccessTokenValid (https://app.powerbi.com/13.0.11674.244/scripts/reportembed.bundle.min.js:21:31523)
at t.promptForLogin (https://app.powerbi.com/13.0.11674.244/scripts/reportembed.bundle.min.js:21:31233)
at m.scope.promptForLogin (https://app.powerbi.com/13.0.11674.244/scripts/reportembed.bundle.min.js:21:25515)
at fn (eval at compile (https://app.powerbi.com/13.0.11674.244/scripts/reportembed.externals.bundle.min.js:1444:307), <anonymous>:4:374)
at m.$digest (https://app.powerbi.com/13.0.11674.244/scripts/reportembed.externals.bundle.min.js:1350:310)
at https://app.powerbi.com/13.0.11674.244/scripts/reportEmbed.min.js:1:1626830
at t.i [as _next] (https://app.powerbi.com/13.0.11674.244/scripts/reportEmbed.min.js:1:189984)
I checked the access token and they appear valid. (No different to the ones working yesterday). I added a debug hook into window.atob and it seems like something inside of parsePowerBIAccessToken is passing undefined to atob. I can't figure out why though unless this code changed.
Kind of stuck on how to figure out the issue. (Not helping that Chrome seems to struggle to debug the lines without crashing).
The code path is trying to run the embed token through this code:
e.prototype.parsePowerBIAccessToken = function() {
return JSON.parse(atob(i.powerBIAccessToken.split(".")[1]))
}
Odd because the code is clearly using "tokenType: models.TokenType.Embed," and thus probably shouldn't be going down that code path?
I noticed it works if I'm logged into the MS account though, so it's using cookies.
If you copy and paste the embed URL from a report it'll have autoAuth=true in the URL. You must remove this from the embed URL or it attempts to use your cookies to authenticate. (It'll also try to use the embed token like an access token and execute wrong code, so that's MS's bug).
In my JS code I removed the autoAuth from the embed url and it'll skip trying to use cookies.
embedURL = embedURL.replace(/autoAuth=true&/ig, '');
You should always get the embed URL using the REST APIs.
From the embed for your customers (Embed Token) documentation
using Microsoft.PowerBI.Api.V2;
using Microsoft.PowerBI.Api.V2.Models;
// You need to provide the workspaceId where the dashboard resides.
ODataResponseListReport reports = await client.Reports.GetReportsInGroupAsync(workspaceId);
// Get the first report in the group.
Report report = reports.Value.FirstOrDefault();
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
EmbedToken tokenResponse = client.Reports.GenerateTokenInGroup(workspaceId, report.Id, generateTokenRequestParameters);
// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = report.EmbedUrl,
Id = report.Id
};
You get the embed URL from the Report object.
The URL you got from powerbi.com is powerbi secure embed and it is not recommended to use this URL for another scenario.
We raised this issue with the PowerBI team. You are supposed to use an API call to get the embed URL for a report. There is an API tester here: https://learn.microsoft.com/en-us/rest/api/power-bi/reports/getreportingroup
Here is a playground for testing embedding: https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html

Run prerequest script to stringify body of the request

I am using post man to send a post request with the body as form-data which contains files and text. See the image below:
I want to json.stringify the entire body but I cannot work out how to do this in a pre-request script. As an environment variable can only be one part of the body further having files makes it more tricky.
I am not sure I understand the problem. In postman the request is a JavaScript object. If you are trying to stringify the request, I assume you are trying to get this:
propertyOne=valueOne&propertyTwo=ValueTwo
out of this:
const request = {
propertyOne: 'valueOne',
propertyTwo: 'ValueTwo'
};
The simple way is just to iterate the object's properties and write into an string:
function stringifyRequest(object) {
let resultString = '';
for (var property in object) {
if (object.hasOwnProperty(property)) {
let tempString = `${property}=${object[property]}`;
resultString = resultString ? `${resultString}&${tempString}` : tempString;
}
}
return resultString
}
Now, if you want to get the binary of the file you are uploading, it will not be possible. As seen in this thread:
We don't give access to the contents of the files in pre-request
scripts, for a few reasons.
We want to delay loading file contents to right before the request
is sent.
The request body is not actually resolved until the pre request
scripts are completed. So even if we wanted to we can't give the
actual body of the request in pre-request scripts.
They may eventually change that, but I could not find any indications of it. One user in this thread suggests using insomnia, you could check it out if fits your needs better.

loopback operation hook: add filter to count api

I need to intercept my loopback queries before they query my Mongodb to add additional filters, for example, to limit the object to what the user has access to.
I can successfully update the query on access operation hook to add filters to the GET /Applications , where Applications is my object. However This fails to work for GET /Applications/count
The command runs with a 200, however it returns zero results, even though I'm adding the exact same filters. There most be something different about count that I'm missing. The ctx object looks have a ton of functions/objects in it. I'm only touching the query property, but there must be something else I need to do.
Any ideas? Thank you, Dan
Could you please share your access hook observer's implementation. I tried it on a sample app, and following access hook works as expected for /api/Books/count:
module.exports = function(Book) {
Book.observe('access', function logQuery(ctx, next) {
ctx.query.where.id = 2; // changing filter value for where
console.log('Accessing %s matching %j', ctx.Model.modelName, ctx.query.where);
next();
});
};
Verify that you're modifying query property of Context (see access hook).
Hope that helps.

JMeter - Verify a Specific Cookie Value was Used?

So in my Test Plan I have a Cookie Manager setup inside my Thread Group which sets a specific Cookie value for 1 Cookie. Let's call it, MYID. I'm trying to figure out a way to verify that this specific Cookie's value was used to complete this one HTTP Request, because if I set my MYID to a specific value *(which actually tells which web server to go to), say to "Server1", but Server1 is down, unavailable, etc... HAProxy should change this and send you to Server2.
So basically I want to try and make sure that Cookie MYID was equal to "Server1" all the way through the HTTP Request.
I am trying to use a BeanShell PostProcessor to verify the Cookie's value after the request is ran, but when I tried using some code I have inside a PreProcessor that sets a cookie in a different Test Plan of mine I get an error saying:
Error Message:
Typed variable declaration : Attempt to resolve method: getCookieManager() on undefined variable or class name: sampler
And below here is the Code slightly modified from a BeanShell PreProcessor in another Test Plan I have...
CODE:
import org.apache.jmeter.protocol.http.control.Cookie;
import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = sampler.getCookieManager();
for (int i = 0; i < manager.getCookieCount(); i++) {
Cookie cookie = manager.get(i);
if (cookie.getName().equals("MYID")) {
if (cookie.getValue().equals("Server1")) {
log.info("OK: The Cookie contained the Correct Server Number...");
} else {
log.info("ERROR: The Cookie did NOT contain the Correct Server Number...");
}
break;
}
}
For the error, I was thinking the "sampler" object was no longer available since the Request was already run, or something along those lines, but I'm not sure...
Or, is there another JMeter object I should be using instead of the "BeanShell PostProcessor" in order to verify the Cookie's value was correct..?
Any thoughts or suggestion would be greatly appreciated!
Thanks in Advance,
Matt
If you trying to get cookie manager from the parent sampler in the Beanshell PostProcessor - you need to use ctx.getCurrentSampler(), not "sampler" as it is not exposed in script variables.
So just change this line:
CookieManager manager = sampler.getCookieManager();
to
CookieManager manager = ctx.getCurrentSampler().getCookieManager();
And your script should start working as you expect.
ctx is a shorthand to JMeterContext instance and getCurrentSampler() method name is self-explanatory.
For more information on Beanshell scripting check out How to use BeanShell: JMeter's favorite built-in component guide.

How can I force ember-data to refresh an object after committing it?

I'm submitting an object to our API via a POST and then transitioning to a route that displays that object. The API modifies one or more fields in the object in the POST and returns the updated info in the request response.
The data displayed is the original data from before the POST to our API. I can see from the console that ember-data is receiving back the updated information from our API. How can I force ember to "refresh" the object so that it displays the correct info?
Matt. Yehuda Katz posted a reply to a user which provides this functionality:
https://stackoverflow.com/a/14183507/506230
Basically you create a record, apply it, save it, then reload it.
saveMessage: function(text){
var acct = Social.Account.find(this.get("id")),
msg = Social.store.createRecord(
Social.Message,
{
text: text,
account: acct,
created: new Date()
}
);
acct.get("messages").addObject(msg);
Social.store.commit();
var timeoutID = window.setTimeout(function(){
__msg.reload();__
console.log('reloading');
}, 250);
}
It turns out ember was actually behaving properly and no additional work was necessary. The problem is that I was setting a variable on the controller with the same name as a computed property on my model. The value of the variable on the controller was being displayed rather than the computed property. Changing the name of the controller variable resolved the issue without any additional code.