3
Given path '/api/metrics/product/ABC'
When method get
   * def id = get response
   * print id
   * def basePathProducts = '/another/api/' + id + '/param'   
  Given path basePathProducts
  When method GET
  Then status 200

12:59:28.447 [main] INFO com.intuit.karate.StepDefs - [print] "5ca627bf3edd851238e59c9e" Apr 16, 2019 12:59:28 PM org.glassfish.jersey.logging.LoggingInterceptor log SEVERE: 2 * Sending client request on thread main 2 > GET

http://localhost:8080/API/ANOTHER/API/%225ca627bf3edd851238e59c9e%22/PARAM

Knight
  • 59
  • 7

3 Answers3

2

Your post is really hard to comprehend.

Try using

Given url yourURLVariable + 'another/api/'+ id + '/param'

Refer to this for more information : https://stackoverflow.com/a/54477346/10791639

Edit : There is a problem with your parameter.

* def id = "5ca627bf3edd851238e59c9e"
* print id

Gives :

13:24:19.783 [print] 5ca627bf3edd851238e59c9e

So your variable id is "5ca627bf3edd851238e59c9e" instead of 5ca627bf3edd851238e59c9e

Adrien
  • 1,080
  • 4
  • 14
  • Thanks for the reply Adrien I couldn't edit the code as I posted it thru my mobile(away from my machine). The response from the first API is a string so how can I escape the double quotes getting added to the second url..I did try the "Given url " I till see the %22 getting added in the url. Thanks in advance. – Knight Apr 16 '19 at 09:53
  • 1
    @Knight I edited my answer. The problem is with the value you get in response, which has the `"` inside the string – Adrien Apr 16 '19 at 11:27
  • 1
    Yes Adrien, I added a JS function to remove the quotes from the string and appended in the URL. – Knight Apr 16 '19 at 18:29
2

I think you are overcomplicating things and you missed that the path syntax is designed for what you commonly need to do.

Don't def basePathProducts and do this, see how the id variable can be easily plugged into a path:

Given path 'another', 'api', id, 'param'
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks for the edit Thomas. The double quotes from the first response( "5ca627bf3ghut56c9e") is getting appended in the URL as %22. How do I skip that? – Knight Apr 16 '19 at 10:49
  • 1
    @Knight `* def id = get response` looks wrong, please look at the docs and examples, there are many ways to extract the data you need cleanly - unless your response is a string - then just do a replace if needed – Peter Thomas Apr 16 '19 at 10:51
  • Oops, didn't notice the "get" came here..it was one of the unsuccessful trial attempts :p. Yes, the response I'm getting is a string. – Knight Apr 16 '19 at 10:57
1
* def newresp = function(id){ return id.slice(1, -1); }
* def id = newresp(response)

I added these to remove the first and last characters from the response which is the double quotes in my case. Thanks for responding guys!

Knight
  • 59
  • 7