Postman - GUIDS - javascript

As part of my API call I need to pass in random ids in the body, currently I am creating the IDs using GUID in C# but its proving to be really time consuming as I need to test APIs in large batches. I am trying to figure out if it's possible to create a GUID in Postman on the fly and save them into a variable and pass it into require parameters through the API call.
I came across few resources like
What is {{$guid}} used for in Postman?
https://www.toolsqa.com/postman/guid-in-postman/
First issue I am having is that I want to create the ID without the dash, so instead of b3d27f9b-d21d-327c-164e-7fb6776f87b0 I want b3d27f9bd21d327c164e7fb6776f87b0.
Secondly, I want to save these into a environment variable and pass it where it's required e.g.
In body as raw text I need to pass:
{
"clientID":{{id}},
"clientpassword":{{password}},
}
In the 'Tests', I currently have this script but I can't manage to get it working.
let id = {{$guid}};
let password = {{$guid}};
I have created 2 global variables called id and password
Also, currently I have these in the 'Tests' section of the postman, I see there is also a pre-request script. In which should I put this?
EDIT
Also, another thing I am trying to do is, sometimes I need to search for a user by providing a client id in different places. Is it possible to put the id and password in one place where I can either update it manually or by running the guid, so that I don't need to update it in each API test I do but rather it should grab it from one place where I updated it.

You do something like this in the Pre-request Script sandbox:
let idGuid = pm.variables.replaceIn('{{$guid}}')
let passwordGuid = pm.variables.replaceIn('{{$guid}}')
pm.variables.set("id" , idGuid.replace(/-/g, ''))
pm.variables.set("password" , passwordGuid.replace(/-/g, ''))
These would then resolve the variables that you have placed within the request body.
Not the most efficient solution but it would create the values you would like.
There are also a number of other fake data variables available for you to use in the application:
https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/#dynamic-variables

Related

Using meteor's simple:rest package in publications with arguments

I a meteor app, using the simple:rest package to access publications works very nicely!
However, I didn't manage yet to GET a publication that requires an argument.
The docs say nothing about publications with arguments.
When I call the GET URL without argument http://localhost:3000/publications/export/, I get an error message (as I check for the argument in my publication)
When I call get URL with attached argument: http://localhost:3000/publications/export/12345/ the main app gets loaded instead (well, with some broken links to images)
When I add an url option to the publication
Meteor.publish("export", function exportPublication(id) {...}, {
url: "export/:0",
})
it doesn't help, I still get the main app.
The example at https://github.com/stubailo/meteor-rest/blob/master/packages/rest/rest-tests.js suggests that using the URL option seems to be the correct way to define a route for a publication with arguments.
What am I missing?
I haven't used this package myself, but I think you need to add the parameter to the publish function, like this:
Meteor.publish("export", function exportPublication(id) {...}, {
url: "export/:0",
})
and you can access id in the code that retrieves the data. If you are treating it as a number, you may need to parse it: parseInt(id, 10)
Also I would use postman or curl to access the URL (it sounds like you are using the browser)
Oh, when I publish the url as export/:0, then it is mapped to http://localhost:3000/export/123 - but not to http://localhost:3000/publications/export/123. I thought the "publications" would be added automatically.

How to save a postman request body in one place and pass when run

How to set the request body in one place and reuse it in multiple requests so I save effort maintaining test scripts if the build changes.
I am using postman for test automation in a dynamically changing environment. the json body structure might change from build to another,
I have to update each request separately.
Here is a sample body where i pass the values from global setter
{ "phone": "{{phone}}",
"income": {{income}}
}
these variables are defined in the prerequest as
pm.globals.set("phone", "xxxxxxxx953");
pm.globals.set("income",10);
TIA
By what you say, I understand you want to have a mutable json body structure defined only in one place and then reuse it by specifying different values on different requests.
You can achieve this, by using the following:
Include the value of a variable in the Body > raw tab from each request you want to configure this way, for instance:
{{rawBody}}
Define the JSON object to be sent in the folder (or even collection) Pre-request Script:
var obj = {
phone: "{{phone}}",
income: "{{income}}"
};
pm.environment.set("rawBody", JSON.stringify(obj));
Finally, on the request Pre-request Script tab specify the values corresponding to the request:
pm.environment.set("phone", "xxxxxxxx953");
pm.environment.set("income", 10);
By this, you can handle many requests and modify their json body text at once. Obviously, if you want to specify different values for each request you will have to specify them on the request Pre-request Script tab.

Mirth channelMap in source JavaScript

In my source connector, I'm using javascript for my database work due to my requirements and parameters.
The end result is storing the data.
ifxResults = ifxConn.executeCachedQuery(ifxQuery); //var is declared
I need to use these results in the destination transformer.
I have tried channelMap.put("results", ifxResults);.
I get the following error ReferenceError: "channelMap" is not defined.
I have also tried to use return ifxResults but I'm not sure how to access this in the destination transformer.
Do you want to send each row as a separate message through your channel? If so, sounds like you want to use the Database Reader in JavaScript mode. Just return that ResultSet (it's really a CachedRowSet if you use executeCachedQuery like that) and the channel will handle the rest, dispatching an XML representation of each row as discrete messages.
If you want to send all rows in the result set aggregated into a single message, that will be possible with the Database Reader very soon: MIRTH-2337
Mirth Connect 3.5 will be released next week so you can take advantage of it then. But if you can't wait or don't want to upgrade then you can still do this with a JavaScript Reader:
var processor = new org.apache.commons.dbutils.BasicRowProcessor();
var results = new com.mirth.connect.donkey.util.DonkeyElement('<results/>');
while (ifxResults.next()) {
var result = results.addChildElement('result');
for (var entries = processor.toMap(ifxResults).entrySet().iterator(); entries.hasNext();) {
var entry = entries.next();
result.addChildElement(entry.getKey(), java.lang.String.valueOf(entry.getValue()));
}
}
return results.toXml();
I know this question is kind of old, but here's an answer just for the record.
For this answer, I'm assuming that you are using a Source connector type of JavaScript Reader, and that you're trying to use channelMap in the JavaScript Reader Settings editing pane.
The problem is that the channelMap variable isn't available in this part of the channel. It's only available in filters and transformers.
It's possible that what you want can be accomplished by using the globalChannelMap variable, e.g.
globalChannelMap.put("results", ifxResults);
I usually need to do this when I'm processing one record at a time and need to pass some setting to the destination channel. If you do it like I've done in the past, then you would first create a globalChannelMap key/value in the source channel's transformer:
globalchannelMap.put("ProcID","TestValue");
Then go to the Destinations tab and select your destination channel to make sure you're sending it to the destination (I've never tried this for channels with multiple destinations, so I'm not sure if anything different needs to be done).
Destination tab of source channel
Notice that ProcID is now listed in the Destination Mappings box. Click the New button next to the Map Variable box and you'll see Variable 1 appear. Double click on that and put in your mapping key, which in this case is ProcID.
Now go to your destination channel's source transformer. There you would enter the following code:
var SentValue = sourceMap.get("ProcID");
Now SentValue in your destination transformer has whatever was in ProcID when your source channel relinquished control.

What is the REST style for getSomethingByAnother() method

I have a Node.js REST API service and I have a resouce - Link for example.
To get all links I use GET /links
To submit new link I use POST /links
To get one link by linkid I use GET /links:id
Each link have an array of Tags and I need a REST style URI to get the links by tag value. What will be REST style URI in this case ?
For getting the links of a certain tag you can define the following route:
/tags/:tagID/links
And for getting tags of a certain link:
/links/:linkID/tags
I think it should be:
GET /links/:id/tags
that should return all the tags related to the link with id ":id"
If you like to work with your tags as a separated thing you could do it like this:
GET /tags ==> retrieve all tags.
GET /tags/:id ==> retrieve tag with id..
GET /tags/links/:id
Also resftull is not strict, and some times, the resource, or action that you need to execute does not fit in that schema, and you can create a custom method:
GET /tags/get-for-link-id/:id => retrieve tags related to a link
That example is pointless, but consider that you are having a complicated route with so much params eg:
GET /tags?q=return&state=active&sort=date if this request is repeated so much times, for your api customer it would be pleasant to have a custom alias like GET /tags/activeByDate
It depends. TYou can do something like /blah:{tagId}/ (which is perfectly valid URI as well). Or you can do /links/?tagID={id} and so on. I don't prefer the already mentioned hierarchical URI /tags/{id}/links, I think a flat URI is much better. If the relationship has attributes as well, then you can use /tag-link-relationships/{relationshipId} or /tag-link-relationships/tag:{tagId}/ or /tag-link-relationships/?tag={tagId} etc...
From client perspective the URI structure does not matter, because the client follows the hyperlinks the service responds with (aka. uniform interface / HATEOAS constraint).
off: Almost every day we got this question, at least try to search next time.

How do I post a tweet using javascript, twitter api and node.js?

Ok...I'm new to this >.<
I have my npm from github.com (node-twitterbot...whose dependency is twit)
I've looked at the twitter api..
What I'm trying to do is add an action which is post a tweet.
I can't seem to find out how to define the string for the actionName (which might be...)
var tweet = ("https://api.twitter.com/1.1/statuses/update.json");
and the actionFunction. Then I need to put it all together to post. Also, I have my instructions written below, however I'm not sure how to apply them. My actionName could be "tweet"? I have no idea how to define my actionFunction either...Can someone explain this? I NEED TO KNOW WHAT TO PUT WHERE. I have the twitterbot.js file open and ready to edit along with with all my oauth keys...access and consumer stuff. Please help anyway you can. I can paste my twitterbot.js file if that helps. Below are the instructions on the npm site reads:
Actions
In order to get your node-twitterbot to actually do something, you need to define actions. It is done through the addAction() method. It takes 2 parameters:
actionName: a string value for the name of an action
actionFunction: a function to be called when a given action is scheduled. (See below for method signature)
So our addAction method might look like this:
Bot.addAction("tweet", function(twitter, action, tweet) {
Bot.tweet("I'm posting a tweet!");
});
The twitter variable passed into the function is the Twit object associated with a given node-twitterbot, and can be managed directly. The same Twit object is available as [TwitterBot].twitter as well.
The action variable passed into the function is the TwitterBotAction created by addAction.
And the tweet object is the tweet passed into the action (if there was one)
TwitterBotActions
addAction() returns a TwitterBotAction object.
var tweetAction = Bot.addAction("tweet", function(twitter, action, tweet) {
Bot.tweet("I'm posting a tweet!");
});
But you will rarely need to directly hold onto the tweetAction directly. You can always get a reference to the action by calling
Bot.actionWithName("tweet");
Which will return the TwitterBotAction object, or null if the name is invalid (or the action already removed)
Again, I'm trying to put all of this together so i can post a tweet using the javascript in node.js Thank you for your time and consideration.

Categories

Resources