首页 > 解决方案 > 字符串形式的 JSON 正文

问题描述

我正在尝试在 POST 方法的 HTTP 请求中发送 json 正文,但问题是某些字符串实际上是字符串形式的 JSON,因此需要一些棘手的引用才能使其正确。

这是完美运行的 CURL 命令: 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/美国广播公司/回购

空手道 POST 请求:给定路径 '/api/v1/runs' 和请求"{ tags: '{"revision":"master"}', wurl: 'https://github.com/abc/repo', params: '{"a":"b"}', type: 'testflow' }"

但会引发错误: features.wapi.post_wrun: wapi.post_wrun.feature:14 - 评估 (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

我尝试了以下方法:

  1. 我已将内容放在 Json 文件中,例如: { "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")

但似乎没有运气,请让我知道是否需要任何棘手的引用来克服这个问题,这在 CURL 中可以正常工作,但在使用空手道的 POST 请求中却不行?

谢谢,

标签: jsonkarate

解决方案


使用texthttps ://github.com/intuit/karate#text

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

另请注意,karate.readAsString()这也可以:https ://github.com/intuit/karate#read-file-as-string

也就是说,我认为-FcURL 中的意思是表单字段,因此您可能需要做一些完全不同的事情:https ://github.com/intuit/karate#form-field

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

依此类推,然后一个post.


推荐阅读