1

Karate version - 1.0.0

I want to get the queryparams and url and want to concat it and save it to a variable.. I'm using following syntax which doesn't work. when I use param in variable it says param not defined. Does anyone have any work around for this? When I use the following -

Given path '/test/1'
    And param product = "abc"
    And param country = "usa"
    * print param

org.graalvm.polyglot.PolyglotException: ReferenceError: "param" is not defined

How do I concat url and param and get in a variable.

Many thanks!

patel
  • 99
  • 2
  • 6

1 Answers1

2

Following is the example with the corresponding request url & query params for which you can execute this scenario to get the response and GET Request as,

Scenario: Sample File
* def ScenarioEngine = Java.type('com.intuit.karate.core.ScenarioEngine');
Given url 'https://reqres.in'
And path '/api/users'
And param page = 2
And param pages = 1
When method get
And print ScenarioEngine.get().getRequest().getUrl()

For which the response would be printed along with the GET url and query param as, [print] https://reqres.in/api/users?pages=1&page=2

  • 1
    actually I don't recommend the internal API for this as it will change. the right way is to use `karate.prevRequest`: https://stackoverflow.com/a/62510900/143475 – Peter Thomas Jan 06 '22 at 02:28
  • Thanks @PeterThomas - It works after the request is executed. Is there a way to get those details before even hitting request? I want to compare before and after both. – patel Jan 06 '22 at 03:16
  • 1
    @patel in my honest opinion that doesn't make sense. you have full control "before" so why do you want to do un-necessary checks ? are you trying to test a testing framework :) but seriously - take a look at the `params` keyword, maybe that is what you want – Peter Thomas Jan 06 '22 at 05:40
  • my requirement is that I want to pass url and query params and then get authorization header based on that and then with that header I want to execute my request. – patel Jan 06 '22 at 05:54
  • anyways, I took params in a diff variable and did concatination... thanks @PeterThomas – patel Jan 06 '22 at 06:27
  • @patel I wish you had mentioned that earlier, that is indeed a legitimate requirement. please read this and try it out and let me know if anything can be improved: https://stackoverflow.com/a/65019538/143475 – Peter Thomas Jan 06 '22 at 09:28