How to pass response from an api to another api?

How to pass response from an api to another api?

1 Like

Can someone please answer this question.

Even i have the same query

Please look into this topic

I don’t know where exactly you want to pass a response - into header, parameter or body?

@Marek Melocik said:
Please look into this topic

I don’t know where exactly you want to pass a response - into header, parameter or body?

My response will be in Json and i want only 1 key and need to pass it in the body of other API

You can use JsonSlurper library to parse JSON String. Example:

import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import groovy.json.JsonSlurper

// create a new instance of JsonSlurper
JsonSlurper parser = new JsonSlurper()

// build first RequestObject
RequestObject ro = new RequestObject("x")
ro.setRestRequestMethod("GET")
ro.setRestUrl("https://s3.amazonaws.com/postman-static-getpostman-com/postman-docs/test_data_file.json")

// send request and get response text
def resp = WS.sendRequest(ro).getResponseText()
// parse response text as JSON
def parsedResp = parser.parseText(resp)

// get parameter you need from JSON response - parse it using dot notation
def param = parsedResp.username[0]

// build a second request and pass a param from 1st request
RequestObject ro2 = new RequestObject("y")
ro2.setHttpBody(param)
ro2.setRestUrl("www.yoururl.com")

WS.sendRequest(ro2)
2 Likes

Got this.

Now how will i send the response to another API HTTP Body

The last part of code handles it - setHttpBody(yourParameter)

@Marek Melocik said:
The last part of code handles it - setHttpBody(yourParameter)

All my API Web Service Request are in Object Repository
So my Second call will have approx 10 inputs in Body, out of which i need 1 input from other API

I see. Then, you can get your RequestObject and edit its body. See this:

There are multiple ways how to solve this. Either you can just append a variable to the end of a body or you can use String.replace() method to replace some placeholder in your object in repository. It depends on your requirements.

setHttpBody.jpg

you are asking about promises. Based on your problem, you already know that API calls are asynchronous. You control async functions by “forcing” them to wait and return their response before proceeding to the next line of code. A promise is an object you use as a wrapper for your API that does this for you. Here’s a basic example:

const apiPromise = () => new Promise((resolve, reject)=> {
  const apiData = myApiCallForCookies()
  resolve(apiData)
})

apiPromise().then(cookies => nextApiCall(cookies))


Thanks

Suresh Prabhu
API Testing Admin

Getting Error in username
username

also error


please help me to solve