3

We have gone through the Karate documentation where we can compare the exact JSON object as a response (which contains multiple data records) but how can we pass and read the JSON in a single scenario?

Below is my sample.JSON and I want to read this in the request payload.

[{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]

I have tried the JSON structure in the above format, however, I am getting below exception. Kindly help me in this.

status code was: 400, expected: 200, response time: 4315

Kindly suggest how to read and pass it in request payload of single scenario.

Thank you.

Madhur
  • 51
  • 3

1 Answers1

1

Status code 400 means you have made some other mistake with the request. Karate is working fine, it is just an HTTP client, maybe the request was not in the "shape" the server was expecting. Talk to the server-side team if you can or check the documentation of the API.

Here is a simple example that works, paste it and try:

* def body = [{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]

* url 'https://httpbin.org/post'
* request body
* method post
* status 200

EDIT: for looping, please read the documentation.

The below example is just one way of doing it - please identify what best you are comfortable with: https://github.com/intuit/karate#data-driven-tests

Feature:

Background:
* def data = [{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]

Scenario Outline:
* url 'https://httpbin.org/post'
* request __row
* method post
* status 200

Examples:
| data |
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thank you Peter for your response. I have tried the above solution. Here, in this case, it is considering a `body` a single object wherein I have passed two objects. `[{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]` So, please suggest to me how can I read an object one by one. – Madhur Apr 28 '20 at 09:08
  • @Madhur I added one more part to my answer. please PLEASE read the docs, take half-day off and do it, it is worth it I promise :) – Peter Thomas Apr 28 '20 at 09:28