I wrote a javascript code that calculates a Fibonacci nth term, how Can I use it in Postman.
I send an initial request to an endpoint and the response sends back data (a n term for example: 43)
I would like to then use this received data(43) in my code to the calculate the answer and send back a post with the answer calculated from my code.
}
• I created an Environment that then has the following variables
VARIABLE INITIAL VALUE CURRENT VALUE
token abc abc
data 123 123
• Within the 1st endpoint I have included a test script as follows so that I automatically
Transfer the data acquired in the first response to the 2nd endpoint and thus a Request response
Chain.
Test script in 1st endpoint:
bodyData = JSON.parse(responseBody)
value = bodyData.token
console.log(value)
pm.environment.set(“token”,value);
value2 = bodyData.data
console.log(value2)
pm.environment.set(“data”, value2);
• These variable are then collected and entered in the request(post/send) in the 2nd endpoint
Using JSON body as content as prescribed:
{
“token”: “{{token}}”,
“result”: “{{data}}”
}
• Thus far it is doing exactly what I need which is to send the first request to the 1st endpoint url
then receive a response with the token key and data to be used for calculation. The issue is that
the new data variable has to be calculated and the output replacing the data variable stored so that it could be used in the second request in the 2nd endpoint.
Please assist in using the following code:
//Fibonacci Series using Recursion
class fibonacci
{
static int fib(int n)
{
if (n <= 1)
a. return n;
return fib(n-1) + fib(n-2);
}
public static void main (String args)
{
int n = 9;
System.out.println(fib(n));
}
}
NOTE THE TOKEN ONLY LASTS FOR 5 SECONDS HENCE WHY I NEED EVERYTHING TO HAMPPENT SWIFTLY AND QUICK.
@Bhekz If you want to use the new calculated ‘data’ value in the 2nd request, you have place your function to calculate the value under the ‘Pre-request Script’ and then set the value back to environments.
It would be good if you share your function for fibonacci series, so i can help in modifying it for your case.