Search code examples
restweb-api-testingkarate

Getting issue while sending nested JSON as request to validate the POST REST API method using Karate framework


Please note that i am trying to send the below request and validate the response on POST method using url

http://uat-ws.*.uk.***.ws/index.php/*/****/validatevoucher

like below: I am using Karate framework here to automate REST API

Scenario: validate voucher

Given url 'http://-..uk../v3//******' 
And header Content-Type = 'application/json' 
And request {json request here}
When method post
Then status 200 



12:17:30.510 [main] DEBUG com.intuit.karate.StepDefs - response time in milliseconds: 579 12:17:30.528 [main] ERROR com.intuit.karate.StepDefs - FAILED, status code was 400, expected 200

[31mFailed scenarios:[0m [31mexamples/users/WSS.feature:3 [0m# Scenario: validate voucher

1 Scenarios ([31m1 failed[0m) 5 Steps ([31m1 failed[0m, [32m4 passed[0m) 0m3.516s

com.intuit.karate.KarateException: status code was 400, expected 200 at com.intuit.karate.StepDefs.handleFailure(StepDefs.java:516) at com.intuit.karate.StepDefs.status(StepDefs.java:447) at ✽.Then status 200(examples/users/WSS.feature:9)

Solution

  • It it pretty clear from the error that the request is hitting the server, but the server is responding with a 400. The Karate test is working fine, but looks like the server was expecting some other JSON or content-type.

    My suggestion is take the help of someone who can look at the server logs, and find out why the server is throwing a 400.

    Edit: After re-reading your question, I'm guessing you are trying to form a nested JSON. Yes the JSON you attempted to use in the script is wrong. Can you try this. Since you have hidden a lot of the actual request it is very hard to see the problem. Please get familiar with JSON and how to create nested JSON like in the example below.

    And request
    """
    { 
      memberId: '******', 
      memberIpAddress: '******', 
      voucherData: { 
        blabla: { 
          voucherNo: '*****', 
          voucherValue: '***', 
          voucherIssueDate': '', 
          voucherExpiryDate: '******' 
        } 
      } 
    }
    """