1

For some reason a variable with a / character get converted to a \/, how do I prevent this?

  1. I start a echo server that listens on localhost:3000 by running npx http-echo-server
  2. I execute the following:

code:

* def CHALLENGE_USER = '/abc/user'
* def loginJson = { user: '#(CHALLENGE_USER)' , name: 'Some Name'}
* print loginJson
* def TEST_URL = 'http://localhost:3000'
Given url TEST_URL+'/session/loginresponse'
And header Content-Type = 'application/json'
And request loginResponseJson
And method put
Then status 200

It prints { "user": "/abc/user", "name": "Some Name" } like I expect.

  1. The http server logs show "--> {"user":"/schemes/ATT_5_55/CH_1","name":"Some Name"}"

  2. Karate shows the result of the echo {"user":"\/abc\/user","name":"Some Name"}

I have tried:

  • def CHALLENGE_USER = '/abc/user'
  • def CHALLENGE_USER = "/abc/user"
  • def CHALLENGE_USER = '/abc/user'
  • def CHALLENGE_USER = '//abc//user'

also setting the variable after the fact does not work:

* def loginJson = {  name: 'Some Name'}
* loginJson.user = CHALLENGE_USER
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
  • 1
    Escaping slashes is redundant but legal. There is no cause for concern. You need to modify your test to take this into account. – Raymond Chen Jul 16 '21 at 14:57

2 Answers2

2

Yes this is legal as per the JSON spec: JSON: why are forward slashes escaped?

And the Java libraries we use does that.

Does your server have a problem ? If so - then you have a bug that Karate surfaced.

And if you really want to have full control over the request, please use text but IMO it may be a waste of time: https://stackoverflow.com/a/68344856/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Oh my, I was not aware of the JSON spec forward slash escaping. It seems like a feature not a bug... :-) – rjdkolb Jul 16 '21 at 14:59
1

A nasty workaround, please forgive me Peter Thomas.

You can convert the json to a string and then remove the \ characters.

I only have one use case for this thank goodness.

* def CHALLENGE_USER = '/abc/user'
* def loginJson = { user: '#(CHALLENGE_USER)' , name: 'Some Name'}
* string json = loginJson
* def loginJsonText = json.replaceAll("\\", "")
* print loginJson
* def TEST_URL = 'http://localhost:3000'
Given url TEST_URL+'/session/loginresponse'
And header Content-Type = 'application/json'
And request loginJsonText
And method put
Then status 200
rjdkolb
  • 10,377
  • 11
  • 69
  • 89