sandy
sandy

Reputation: 65

JSON body in string form

I am trying to send json body in HTTP request for POST method, but the problem is some of the strings are actually JSON in string form, so there's some tricky quoting needed to get it right.

Here is the CURL command that works perfectly fine: curl -X POST https://www.example.com:8080/api/v1/runs -H "accept: application/json" -H "Content-Type: multipart/form-data" -F tags='{"revision":"master"}' -F params='{"a":"b"}' -F type=testflow -F wurl=https://github.com/abc/repo

Karate POST request: Given path '/api/v1/runs' And request "{ tags: '{"revision":"master"}', wurl: 'https://github.com/abc/repo', params: '{"a":"b"}', type: 'testflow' }"

But throws an error: features.wapi.post_wrun: wapi.post_wrun.feature:14 - evaluation (js) failed: "{ tags: '{"revision":"master"}', wurl: 'https://github.com/abc/repo', params: '{"a":"b"}', type: 'testflow' }", <eval>:1:12 Expected ; but found revision "{ tags: '{"revision":"master"}', wurl: 'https://github.com/abc/repo', params: '{"a":"b"}', type: 'testflow' }" ^ in at line number 1 at column number 12

I have tried the following:

  1. I have put the content in Json file for example: { "url": "https://github.com/abc/repo", "type": "testflow", "tags": { "revision": "master" }, "params": { "a": "b" } }

def readJsonbody = read ("../test/feature/test.json").

  1. def readJSOnbody = karate.readJsonbody("classpath:test.json")

But seems like no luck, Please let me know if there is any tricky quoting needed to overcome this issue, which works fine in CURL but not in POST request using Karate?

Thanks, S

Upvotes: 1

Views: 1424

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58153

Use text: https://github.com/intuit/karate#text

* text body =
  """
  { tags: '{"revision":"master"}', wurl: 'https://github.com/abc/repo', params: '{"a":"b"}', type: 'testflow' }
  """
* request body

Also note that karate.readAsString() would have worked as well: https://github.com/intuit/karate#read-file-as-string

That said I think -F in cURL means form-fields, so you may need to do something completely different: https://github.com/intuit/karate#form-field

* form field tags = '{"revision":"master"}' 
* form field params = '{"a":"b"}'

And so on and then a post.

Upvotes: 1

Related Questions