Katalon Analytics (beta) – Bringing new insights into test reports. Learn more!

How to pass response from an api to another api?

How to pass response from an api to another api?

Comments

  • Can someone please answer this question.

    Even i have the same query

  • edited October 6

    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

  • edited October 9

    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)
    
  • 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.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!