Generic request on Postman - rest

I want to use Postman to test the REST API of a project, and I'm trying to a generic request. For example:
I have a POST request that makes a research, the parameters of this research
are in the body. How do I automate the request with a pre-request-script that modifies the body at each iteration?
Because I don't want to create a collection with 20 POST requests where only the body changes. I tried to use a while loop in pre-request-script or in test script but Postman executed the request only once. Then I tried to use the runner for calling the request 5 times, for example, and i wrote a script that modified a variable each time, but I think that global and environment variable are reset at each iteration of the runner.
Do you have any ideas?

enter image description here
I tried that to modify value of password when I use the runner. But if the runner reset variables at each iteration it can't work.
It's ok I found an other solution:
For example I want to test the request with 5 different password, i create 5 file json who correspond to my global variables or environment variables and in this files I modify just one variable. I use newman i a loop script to call the same request with different global variable file to modify just one parameter of the request.

You can also use set_nextRequest() (see http://blog.getpostman.com/2016/03/23/conditional-workflows-in-postman/). With this you should be able to force calling your request again with the new value for your password and not perform multiple iterations. If you use a global variable in your json body and you update it in your loop, it should work.
hope this helps
Alexandre

Related

Default preset of headers in Postman

When manually testing requests in Postman, is there a way to define some set of default headers to be used for each request (without choosing manually Preset before sending each request)?
I.e. is there a way to define a default Preset to be used?
Additionaly when I follow some of the links in the response (by clicking on it) and send a request for this link, the headers from the original request are lost. Is there a way to preserve them automatically?
Thanks in advance
There is no direct way to do this but this can be done via Collection level Pre-request script. Steps-
Create a collection in the postman and put all requests in which you want the common headers.
Edit the postman collection.
Write this in your Pre-request Script section.
var Header = require('postman-collection').Header;
pm.request.headers.add(new Header("foo:foo"))
pm.request.headers.add(new Header("bar:bar"))
As of 7.0.9 - You should be able to now do this in your Pre-request Scripts.
You can use the following syntax:
pm.request.headers.add({key: 'header_name', value: 'header_value' })
pm.request.headers.upsert({key: 'header_name', value: 'header_value' })
pm.request.headers.remove('header_name')
Tips: Fork this collection to see how it works directly in Postman:
https://www.postman.com/postman/workspace/postman-answers/collection/9215231-ef055751-7385-45b4-a6f9-91bbd1c47fa5?ctx=documentation

Syntax to use parameters in Body section of HTTP Request Sampler in JMeter, using Rest API POST request with body and input from CSV file

I'm using JMeter to test a microservice and I need to use a parameter dynamically with a different value in each request. Also, the parameter is a part of a query that contains other constant values as well.
I defined user variables in the JMeter user.properties file (in JMeter bin folder):
JMeter -- bin/user.properties
# Parameters to use in JMeter
ES_HOST=127.0.0.1
ES_PORT=9200
ES_INDEX=segments
ES_TYPE=_doc
THREAD=5
CSVDATA_ROOT=C:/devtools/apache-jmeter-5.2.1/csv_data
Of course, I have User-Defined Variables:
And how my Test Plan is defined in JMeter
As you can see in the following screenshot of View Result Tree the parameter agentName I defined and shown in the HTTP Request (above) is working.
I want to define it in the body of the HTTP Request, to replace the hardcoded "John Doe" with a parameters that have a different value in each request.
"query":"SearchStartTime=2020-01-01 00:00:00.000TO2020-01-31 23:59:59.999&AgentName=John Doe"}
How can I do that?
I need a way to add a parameter to an existing string
I've already tried Using Apache JMeter to Test Elasticsearch (or any REST API) and In Jmeter, What would be syntax of parameters in Body Data section of HTTP Request Sampler, for Rest APIs and input should be generated dynamically also doesn't solve my problem.
Use same syntax as HTTP request - ${agentname} for getting variable value:
"query":"SearchStartTime=2020-01-01 00:00:00.000TO2020-01-31 23:59:59.999&AgentName=${agentname}"

How to keep the same session between two REST request in ReadyAPI?

I have two REST requests in the same TestCase.
login request
getAllParameters request
I am currently using ReadyAPI 2.6.0.
I need to log in the first request in order to access the second one.
Otherwise it does not work.
Do you know how to keep the same session between these two requests?
I'm guessing that the key you need to use is set in the HTTP headers.
Given that is true, and that you also need to set this particular value in the HTTP header in the following requests, you may solve it like this:
In your first REST Request, you add a Script Assertion with the following code:
def value = messageExchange.responseHeaders["session-id"];
assert value != null
assert value.size() == 1
context.setProperty("sessionID", value)
You will need to substitute "session-id" in the first line with whatever name your correct HTTP header has.
You should NOT change the "sessionID" in the last line. This is a separate variable name we use for ourselves.
This will assert that a value has been set, and will then save it as a context variable, which we can reuse in later steps.
Add a Groovy Script teststep after your first REST Request Teststep. Rename it to "Extract Context Variable" (that name will be reused in the next step)
Then add this code in it:
def value = context.getProperty("request-id")
return value
Context values are not available from anywhere. By extracting it here, and returning the value, it will be easier to make use of it in the rest of your REST Request teststeps.
Open your second REST Request Teststep (and third, and fourth etc. if you have more)
Open the Headers pane at the bottom
Create a new key named the same as the header your received in your first REST Request
In the value, you enter
${Extract Context Variable#result}
When running the entire testcase, you should now automatically retrieve the header returned in the first response, and then transfer and reuse it in the following requests.

Chaining postman requests - call postman request from another request?

I have two postman requests x,y which hit endpoints in two different rest api X,Y. x will give me an authentication token which is necessary to make the y request. How do I make the request x inside request y ? That is, call x from inside y.
PS - I have already figured out how to use request x to setup environment variables (auth token) which will be picked up by y.
If you want to do it in one shot, you can use the pre-script option.
Pre-script are triggered before the request. So if you make a request and set the token in an environnement variable, the next call can use the token. You should check that page. It seems outdated, you should replace pm by postman.
You might also want to add a pre script request to your collection. This way, every request of that collection will be precede by a request to the authentification route for exemple.
Have a look at this:
https://blog.postman.com/conditional-workflows-in-postman/,
if you want to call a specific request from within a request,
the postman.setNextRequest() function will do the job.
Alexandre
The official documentation has you covered big dog - Branching and looping.
When running a collection, you can branch and loop across API requests
in Postman using the postman.setNextRequest("request_name"); function.
Then you use the collection runner to run the API requests in sequence.
Think about using the Collection Runner to execute a sequence of requests.

Using variables in SOAP request file in JMeter

In a JMeter (v2.13) test plan I have a SOAP/XML-RPC sampler. The SOAP request itself is loaded from a random file.
Sample request
<mySoapRequest>
<value>555</value>
</mySoapRequest>
This works fine.
I would now like to replace this fixed value with a variable which is defined in JMeter, i.e.
<mySoapRequest>
<value>${someValue}</value>
</mySoapRequest>
It seems as if JMeter does not resolve this variable. The actual SOAP request sent to the service does not contain 555 but ${someValue}. Is there any workaround so that I could use variables in the file?
That can be done using FileToString and eval functions.
For this XML,
<mySoapRequest>
<value>${someValue}</value>
</mySoapRequest>
In the SOAP/XML RPC request Data section, use the functions as shown below to get the value replaced at run time.
${__eval(${__FileToString(C:\users\me\desktop\soap.xml)})}
__FileToString - The FileToString function can be used to read an entire file. Each time it is called it reads the entire file.
__eval - The eval function returns the result of evaluating a string expression.