json - 字符串形式的 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
我尝试了以下方法:
- 我已将内容放在 Json 文件中,例如:
{ "url": "https://github.com/abc/repo", "type": "testflow", "tags": { "revision": "master" }, "params": { "a": "b" } }
def readJsonbody = read ("../test/feature/test.json").
def readJSOnbody = karate.readJsonbody("classpath:test.json")
但似乎没有运气,请让我知道是否需要任何棘手的引用来克服这个问题,这在 CURL 中可以正常工作,但在使用空手道的 POST 请求中却不行?
谢谢,
解决方案
使用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
另请注意,karate.readAsString()
这也可以:https ://github.com/intuit/karate#read-file-as-string
也就是说,我认为-F
cURL 中的意思是表单字段,因此您可能需要做一些完全不同的事情:https ://github.com/intuit/karate#form-field
* form field tags = '{"revision":"master"}'
* form field params = '{"a":"b"}'
依此类推,然后一个post
.
推荐阅读
- javascript - 如何将 Material-table 与 Material-UI Dialog 结合起来?(反应JS)
- python - 有没有办法在 DJANGO 的模板中显示元素的计数器?
- javascript - 尝试安装 node-jasper 时出错
- pandas - 用于绘图的中心调色板 - Seaborn
- html - 无法下载文件:我正在使用锚标签下载文件,但本地主机附加到它并且链接混乱
- r - R:速度/聚合 - 在特定时间段内每列 A 的列 B 的唯一计数过多?
- nginx - nginx url重写失败
- javascript - 如何在引导程序 maxlength 上手动设置当前键入的字符?
- ios - 如何使用带有 ALL ... IN 格式字符串的谓词获取数据
- c# - 在 C# 中使用 S7NetPlus 库的 PLC 通信错误