1

I have variable cardholder in my karate-config file.

I assigned it to the new entrID variable.

enter image description hereThe main thing that i am building JSON as a String..

 * def entrID = cardholder  
 * def requestContactHistoryAdd = 
   """ 
   { 
   "RequestBody": "{ \"ENTR_ID\" : \"entrID\", \"BHVR_ID\" : \"VRU\", }"
   } 
   """

Now how can i provide it inside of my json RequestBody?

2 Answers 2

1

EDIT: since you seem to have a very badly designed API where the JSON has an embedded string (which looks like JSON).

Please note I am using a string type below: https://github.com/intuit/karate#type-conversion

You can do this:

* def entrID = 'foo'  
* string temp = { "ENTR_ID" : "#(entrID)", "BHVR_ID" : "VRU" }
# note that you could have done this: 
# def temp = '{ "ENTR_ID" : "' + entrID + '", "BHVR_ID" : "VRU" }'
* def body = { RequestBody: '#(temp)' }
* print body

Which gives you:

08:17:25.671 [main] INFO  com.intuit.karate - [print] {
  "RequestBody": "{\"ENTR_ID\":\"foo\",\"BHVR_ID\":\"VRU\"}"
}
Sign up to request clarification or add additional context in comments.

3 Comments

I agree that it is ugly to build json as a string ... but the endpoint developed as follows..and not accept another way of the body ... that is the problem. I saw this way in the documentation. I will try to play, maybe I can solve it somehow. thanks
@SaidYusifli ok, I completely changed my answer. does it work for you ? but please be more clear in your question next time.
wow this is really cool. Thank you so if i declare my variable as a String not def. its returning with all \" \".
1

i solved it also like this

 * def entrID = someValueFromSomeWhere
 * def bodyValue = "{ \"ENTR_ID\":\"" + entrID + "\", \"BHVR_ID\" : \"VRU\" }"
 * def requestContactHistoryAdd = 
   """ 
   { 
   "RequestBody": "#(bodyValue)"
   } 
   """

we can also do this way

 * def bodyValue = "{ \"ENTR_ID\":\"" + someValueFromSomeWhere + "\", \"BHVR_ID\" : \"VRU\" }"
 * def requestContactHistoryAdd = 
  """
  { 
  "RequestBody": "#(bodyValue)" 
  }
  """

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.