How to handle parameters in the URL using PATH in Karate DSL framework?

Viewed 1157

I have read the documentation on: https://github.com/intuit/karate#path

I have also read many answers on related subject on these forums, most notably: How to dynamically create URL having path in between URL using Karate framework

However, I still cannot get my head around this concept. Perhaps I am even more a newbie than a typical newbie. My problem is this:

The complete api: /sample/api/v1/sampleweb/{sampleweb}/webversion/{version}

    Feature:
    
        Background:
            * def baseUrl = '/sample/api/v1/'
    
        @postRandomData
        Scenario: POST API for creating data
            Given url host
            And path baseUrl

How in the world can I add the rest of the Url to "baseUrl" (basically the complete path listed above)? Should I throw "sampleweb/{sampleweb}/webversion/{version}" into a variable and then just do "baseUrl + variable"?

Please advise.

2 Answers

@hungryhippos what i do to have this kind of flexibility is to use something like:

var endpoint =  '/sampleweb/{sampleweb}/webversion/{version}'
.replace('{sampleweb}', param1)
.replace({version}', param2)

Just use string concatenation. Just like normal JavaScript. Here try to spot the difference between "hard coded" strings and variables.

* def want = 'something'
* url baseUrl + '/anything'
* path 'you', want
Related