字符串形式的JSON正文 [英] JSON body in string form
问题描述
我尝试在POST方法的HTTP请求中发送json正文,但问题是某些字符串实际上是字符串形式的JSON,因此需要一些技巧的引号才能正确。
下面是可以很好地工作的curl命令: Curl-X POSThttps://www.example.com:8080/api/v1/runs-H&Quot;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 空手道发帖请求: 给定路径‘/api/v1/runs’ 并请求"{ tags: '{"revision":"master"}', wurl: 'https://github.com/abc/repo', params: '{"a":"b"}', type: 'testflow' }"
,但引发错误:
Feature ures.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' }"
^在第1行第12列第1行
我尝试了以下方法:
- 我已经将内容放在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请求中不起作用?
谢谢, %s
推荐答案
使用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
我想cURL中的-F
表示表单域,因此您可能需要做一些完全不同的操作:https://github.com/intuit/karate#form-field
* form field tags = '{"revision":"master"}'
* form field params = '{"a":"b"}'
依此类推,然后是post
。
这篇关于字符串形式的JSON正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!