I wrote a javascript code that calculates a Fibonacci nth term,how Can I use it in Postman

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.

P.S Im new with API etc

@Bhekz
You have to place your java script code in the “Test” tab of the request. Refer this page more info: https://learning.postman.com/docs/postman/scripts/intro-to-scripts/

  1. Use pm.response.json() to get the value (ex., 43 in your case)
  2. Use your fibonacci function to calculate the output.
  3. Save the ouput to the environment using pm.environment.set()
  4. Create a new request and get the value from the environments and use it further.
2 Likes

Thank you please bear with me if I come back with more questions.
Really appreciate the help.

• This is the request I post/send to the 1st endpoint using JSON body as Content as prescribed:

  1. {
  2.  "fullName": "jason",
    
  3.  "email": "jason@testing.com"
    
  4. }

• I then receive this response:

  1. {
  2.  "token": "dTh7Pef1wPA8Ngvl-1Fjjk",
    
  3.  "challenge": "FIBONACCI",
    
  4.  "data": 75
    
  5. }
    • 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:

  1. bodyData = JSON.parse(responseBody)

  2. value = bodyData.token

  3. console.log(value)

  4. pm.environment.set(“token”,value);

  5. value2 = bodyData.data

  6. console.log(value2)

  7. 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:

  1. {

  2. “token”: “{{token}}”,

  3. “result”: “{{data}}”

  4. }
    • 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:

  5. //Fibonacci Series using Recursion

  6. class fibonacci

  7. {

  8. static int fib(int n)

  9. {

  10. if (n <= 1)
    a. return n;

  11. return fib(n-1) + fib(n-2);

  12. }

  13. public static void main (String args)

  14. {

  15. int n = 9;

  16. System.out.println(fib(n));

  17. }

  18. }
    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.

here is the function I would like to use to calculate the output : Java
//Fibonacci Series using Recursion

class fibonacci

{

static int fib( int n)

{

if (n <= 1 )

return n;

return fib(n- 1 ) + fib(n- 2 );

}

public static void main (String args[])

{

int n = 9 ;

System.out.println(fib(n));

}

}

here is the function I would like to use to calculate the output : C++
#include<bits/stdc++.h>

using namespace std;

int fib( int n)

{

if (n <= 1)

return n;

return fib(n-1) + fib(n-2);

}

int main ()

{

int n = 9;

cout << fib(n);

getchar ();

return 0;

}

Try using the below as a pre-request script in your 2nd request.

function fib(n) {
if (n <= 1){
return n;
}
else{
return fib(n-1) + fib(n-2);
}
}
var oldValue = pm.environment.get(“data”);
var newvalue = fib(oldValue);
pm.environment.set(“data”, newvalue);

I used the pre-request script you suggested however I receive the following message:

There was an error in evaluating the Pre-request Script: RangeError: Maximum call stack size exceeded

Tests script running Error: sandbox: execution interrupted, bridge disconnecting