How get variable from request in pre-request script? - postman

1) I set environment variable:
title = "foo-bar"
Picture:
2) I set Request Post parameter
title = {{title}}
Picture:
3) I send POST and a need find title in pre-request script:
pm.request.body.urlencoded.all().forEach(param=>{
if(param.key == "title")console.log(param.value);
})
But in log : {{title)) , I need foo-bar
I want get title from request, dont from pm.variables.get("title")

Related

Postman: Is it possible to update url of postman request in the pre-requisite script

Is it possible to update url of postman request in the pre-requisite script.
I want to edit the url based on dynamic environment input.
For example:
if (environment.someValue) {
request.url = request.url + "\" + environment.someValue
if (environment.anotherValue) {
request.url = request.url + "\" + environment.anotherValue
}
}
console.log(request.url);
The above code gives me prefect console log:
e.g. if url is https://someServer/someRequest, environment.someVar is x and environment.anotherVar is y the console.log(request.url) above prints:
https://someServer/someRequest/x/y
But the problem is (say if i am requesting a Get), even after logging the overridden request url, it only calls https://someServer/someRequest and does not override to https://someServer/someRequest/x/y.
Any ideas how to modify the url as asked above.
if your url in your request is set as a global, it should work.
ie. I have a get request :
GET http://{{myurl}}/etc. with myurl set as a global variable
In my prerequest script I do pm.globals.set("myurl", <new url>);
when I launch my request, it tries to do the GET request on my new url.
So it is possible to do it but you have to use global or environment variables to dynamically update your request:
set your 'someRequest' as a global that you can update in your prescript (instead of request.url), then it will be interpreted when you launch your request
https://someServer/{{someRequest}}
pm.request.url= "dynamic value"
you and update url using the above single line in postman prerequisite step
pm.request.url returns a object with fields host,path and params . But you can replace it with string.
Thanks for the tips: I used these lines of code to manipulate two values that appear in a RESTful API route:
// Ensure Payroll ID and Employee ID in request URL are correctly padded with zeros
pm.request.url.path[pm.request.url.path.indexOf("{{PayrollID}}")] = data.PayrollID.toString().padStart(6, '0');
pm.request.url.path[pm.request.url.path.indexOf("{{EmployeeID}}")] = data.EmployeeID.toString().padStart(7, '0');

Retrieve variable value from API response( in xml format) and set it to environment variable in postman

I am testing API in Postman.
I want to fetch variable from postman response which is in XML format and set
that values as the environment variable.
My postman request response is like below.
<bmi version="2.0">
<job id="2031012"></job>
</bmi>
Here BMI and JOB are tags
I want the value of ID to set an environment variable and use it for another
api test as an input parameter.
I use following code in the Test scripts
tests["Status code is 200"] = responseCode.code === 200;
tests["Body matches string"] = responseBody.has("id");
var responseJson = xml2Json(responseBody);
console.log(responseJson);
postman.setEnvironmentVariable("id",responseJson.id);{code'enter code here'}
There is a blog on the postman website showing how to do this, Extracting data from responses and chaining requests
It looks like you are mostly there and just need to retrieve the value, something along the lines of
postman.setEnvironmentVariable("id",responseJson.bmi.job.id);

Connect to page with table Confluence with Python

I have started a server using Confluence on Centos and have created one page with a table.
Now I want to connect to my page then parse html there and find row&columns but I cannot connect to the page.
My page is located on: http://localhost:8090/display/TEST/Confluence
How can I connect to my page and parse the HTML?
You can use a confluenca api to get the page ID
from atlassian import Confluence
space = '~MYSPACE'
title_parent = 'PARENT_PAGE_ID'
p_id = confluence.get_page_id(space, title_parent)
print(p_id)
title = 'New page'
body = 'This is the body of a new page'
status = confluence.create_page(space, title, body, parent_id=p_id, type='page',
representation='storage')
print(status)
Take a look at Atlassian Example here. For updating your page, you need to know your page ID.
It is better to make two request. The first will be a search that will return you the ID of the page, while the latter will return for its contents.
Search
import requests
url = confluence_host + '/rest/api/content/'
res = requests.get(url=url + 'search',
params={'cql': 'space="TEST" AND title="Page Titile'})
page_id = res.json()['results'][0]['id']
Get HTML
import requests
url = confluence_host + '/rest/api/content/'
page = requests.get(url=url + page_id,
params={'expand': 'body.storage'}).json()
html = page['body']['storage']['value']

Updating ESRI Rest services with post

I am atempting to post a new url to a service on ESRI (I own it) with a post using Requests. After printing the line post_url the JSON is updated as I want it however when I post it nothing happens despite getting a 200 status. Is the issue with the post or the find / replace?
json_url = "https://www.arcgis.com/sharing/rest/content/items/serviceID?&token=XXX"
update_url = "https://www.arcgis.com/sharing/rest/content/users/USERNAME/folder/items/ServiceNumber/update?"
get_json = requests.get(json_url)
load_json = str(get_json.json())
find = "findme"
replace = "replace"
post_url = load_json.replace(replace, find)
a = requests.post(update_url, data={"url": post_url, "token": "XXXX", "f":"json"})
print a.status_code
The issue is with the post
I changed the post to this:
requests.request("POST", update_url, data={"url": post_url, "token": token, "f":"json"})
update_url needs to be the API update endpoint:
https://www.arcgis.com/sharing/rest/content/users/USERNAME/FOLDER/items/Endpoint /update?"
post_url needs to be: "whatever you want" in my case It was a search and replace variable of the the existing URL in the JSON and update, because of a server migration.

How to get Request property in SOAP UI for REST service test

I created a simple project in SOAP UI, and now am trying to extract some test suit properties using Setup Script. Only thing is, RawRequest and Request properties are empty, but I want to display them. Request does exist, but request and raw request properties are null. This is only case with REST services.
def tcaseList = testSuite.getTestCaseList();
// for each testCase
def countTestCases = tcaseList.size();
for(tc = 0;tc<countTestCases;tc++){
def countTestSteps = tcaseList[tc].getTestStepList().size();
for(i=0;i<countTestSteps;i++)
{// get testStep
def testStep = tcaseList[tc].getTestStepAt(i);
runner = tcaseList[tc].run(new com.eviware.soapui.support.types.StringToObjectMap(), false);
def request = testStep.getPropertyValue("RawRequest");
log.info(request.toString())
Any ideas why this property is null, and how to extract request and display it.
In REST Request on SOAPUI the RawRequest property is only available on REST Request which use POST which sends data not only parameters, in REST Request which use GET the RawRequest is empty. If you want instead to get the value of the GET parameters you can use this in your code:
...
def paramValue = testStep.getPropertyValue("parameterName");
...
In REST Request which use POST and sends data you're already doing correctly with your code:
def request = testStep.getPropertyValue("RawRequest");
I add the follow images to illustrate what I'm explaining:
POST with not empty Request:
GET with empty Request:
UPDATE
So if you want to get all the parameters from your REST Requests you can add the follow groovy snippet to your code:
// your for loop...
for(i=0;i<countTestSteps;i++){// get testStep
def testStep = tcaseList[tc].getTestStepAt(i)
...
// code to get REST Request parameters and log to the console
// to avoid errors if you've testSteps which doesn't are RestTestSteps (i.e groovy...)
if(testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep){
// ? is groovy operator to avoid NPE
def parameters = testStep?.getTestRequest()?.getParams()
// loop through the map parameters
parameters.each { k,v ->
// for each params print the name and the value
log.info "$k : ${v.getValue()}"
}
}
...
If you need more info take a look at RestTestRequestStep API.
Hope this helps,