How to store a collection variable from response body like session token which is dynamic and use it in another request ?
i want to store the session Id data and user id data and use it in
I think you are missing a property “response”.
Example:
Output:
Once you save it as a collection variable like in your example you would need to use the pm.collectionVariables.get("variable_key");
option to grab it in the pre-req script of your other calls.
thank you @w4dd325 Postman API Tester
I have another doubt
When I try to get the collection variable from pre-req scrip I am getting 403 error
Could you tell what i am missing
could you provide more info?
maybe screenshots of what you are trying?
Thanks for your response @w4dd325
I figured it out myself
The problem is I didn’t pass the token value So I got 403 error
Thanks for your time
Hello, everyone. I am facing the error “TypeError: Cannot read properties of undefined (reading ‘find’)”. Does anyone please help me with this?
hi @chi.pham ensure each request has at least one test
This one is not quite so easy to troubleshoot as that “test” has a fair bit of code. It doesn’t just contain a single find request which would make it easy to work out which bit is failing.
pm.test("Tests added correctly", () => {
let newFlowFolder = scenarioFolder.item.find(fol => { return fol.name === "New user workflow" })
let firstRequest = newFlowFolder.item.find(req => { return req.name === "User Login" | req.name === "User Login Copy"})
let firstTest = firstRequest.event.find(event => {return event.listen === "test"})
pm.expect(firstTest.script.exec.toString(), 'check first test').to.include("pm.test")
let secondRequest = newFlowFolder.item.find(req => { return req.name === "Account summary" | req.name === "Account summary Copy"})
let secondTest = secondRequest.event.find(event => {return event.listen === "test"})
pm.expect(secondTest.script.exec.toString(), 'check second test').to.include("pm.test")
let thirdRequest = newFlowFolder.item.find(req => { return req.name === "User Logout" | req.name === "User Logout Copy"})
let thirdTest = thirdRequest.event.find(event => {return event.listen === "test"})
pm.expect(thirdTest.script.exec.toString(), 'check third test').to.include("pm.test")
let fourthRequest = newFlowFolder.item.find(req => { return req.name === "Account summary 2"})
let fourthTest = fourthRequest.event.find(event => {return event.listen === "test"})
pm.expect(fourthTest.script.exec.toString(), 'check fourth test').to.include("pm.test")
pm.expect(fourthTest.script.exec.toString(), 'check 403').to.include("403")
pass += 1
})
When you get “Cannot read properties of undefined (reading 'find)”. It means the object that its trying to perform the find against is undefined.
Let’s take the first line as an example.
let newFlowFolder = scenarioFolder.item.find(fol => { return fol.name === "New user workflow" })
If the error was being caused by this line, it would actually mean that “scenarioFolder.item” can’t be found. This object should be an array (as the find function works against arrays).
scenarioFolder is set earlier (and I don’t think its this one that its failing on).
let scenarioFolder = collection.item.find(fol => {return fol.name === "API tests"})
Usually, this is just a problem with the naming of a folder or request. So please check the spelling. You can see within the tests what the folder or request names should be. Although on face value, I can’t see what is wrong with your folder structure. I would perhaps clone the submit collection request and add a few console logs within the tests to double check that the variables are returning correctly. newFlowFolder, firstRequest, firstTest, etc.
Hello,
Help, I’m unable to connect (at step 5), I’ve even tried hard-coding the variables and I always get the same result
Also, when I create a username and password, I randomly get the following error message
At the end I didn’t found such “Account summary 2” in " The Good Bank APIs
collection"
Thanks
This should really be its own question.
The login step is failing, as you have to send the same username and password that was used in the create user request. Not a dynamic variable.
The create user is failing because the username and password need to match the format that was in the example request that you copied over. (Which means you can’t use dynamic variables as they would appear to not be in the correct format).
Account Summary 2 is just a clone of Account Summary. Just copy that request and rename it.
To store a dynamic session token or any other data from a response body and use it in another request, you typically have to follow these steps, depending on the context and the tools you are using (e.g., programming language, API testing tool, etc.):
- Extract the Data:
- Identify the part of the response body where the session token and user ID data are located. This could be in JSON, XML, or any other format.
- Store the Data:
- Capture and store the dynamic data in a variable. The variable should be accessible in the scope of your testing environment or script. You can use the built-in variables provided by your testing tool or create custom variables if needed.
- Use the Data in Another Request:
- In your subsequent request, replace the placeholders with the values stored in the variables. This typically involves concatenating or substituting the variable values into the request parameters or headers.
Here’s a high-level example in a pseudo-code format:
pythonCopy code
# Send a request to obtain the session token and user ID
response = send_request("https://example.com/login")
# Extract the session token and user ID from the response body
session_token = extract_value_from_response(response, "session_token")
user_id = extract_value_from_response(response, "user_id")
# Store these values in variables
store_variable("session_token", session_token)
store_variable("user_id", user_id)
# Use the session token and user ID in another request
new_request = create_request("https://example.com/secure_data")
set_request_header(new_request, "Authorization", "Bearer " + session_token)
set_request_param(new_request, "user_id", user_id)
# Send the new request
response2 = send_request(new_request)
Please note that the specific code and methods used to extract, store, and use data may vary depending on the tool or language you are using for your testing or development. Make sure to consult the documentation of the tool or programming language you’re working with for the exact implementation details.
I found my mistake it was ‘$’ before variables in the body
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.