我想使用cURL命令通过POST将JSON数据发送到服务器。用于创建JIRA问题。
我正在使用groovy代码生成这些命令。但是,创建的cURL命令可以正常工作,并且如果我在控制台中执行它,也可以完全满足我的要求。如果我以常规方式执行它,它将不起作用。
这是我尝试执行cURL命令的常规代码:
def builder = new JsonBuilder()
def root = builder.fields {
/* setting up JSON Builder */
}
String curlStr = "curl -D- -u user:pwd -X POST --data \"${builder.toString().replace('"', "\\\"")}\" -H \"Content-Type: application/json\" ${BASE_URL}/rest/api/2/issue/"
Process response = curlStr.execute()
response.waitFor()
// printing error
print("response.error=${response.err.text}")
print("response=${response.text}")
// printing curl string
print("curlStr=${curlStr}")
输出如下:
response.error= % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: not curl: (6) Could not resolve host: set.\",\"description\" curl: (6) Could not resolve host: of curl: (6) Could not resolve host: an curl: (6) Could not resolve host: issue curl: (6) Could not resolve host: using curl: (6) Could not resolve host: project curl: (6) Could not resolve host: keys curl: (6) Could not resolve host: and curl: (6) Could not resolve host: issue curl: (6) Could not resolve host: type curl: (6) Could not resolve host: names curl: (6) Could not resolve host: using curl: (6) Could not resolve host: the curl: (6) Could not resolve host: REST curl: (3) [globbing] unmatched brace in column 22 curl: (3) [globbing] unmatched close brace/bracket in column 9 curl: (6) Could not resolve host: application 100 64 0 0 100 64 0 1974 --:--:-- --:--:-- --:--:-- 1974
response=HTTP/1.1 415 Date: Thu, 13 Jun 2019 11:47:58 GMT Server: Apache/2.4.18 (Ubuntu) X-AREQUESTID: 827x52078x1 X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN Content-Security-Policy: frame-ancestors 'self' X-ASEN: SEN-13974383 X-Seraph-LoginReason: OK X-ASESSIONID: 126fo6k X-AUSERNAME: user Content-Type: text/html;charset=UTF-8 Content-Length: 0 Set-Cookie: JSESSIONID=9E8CEDF3CC7FC1BC41BD98A32771A0D0;path=/;HttpOnly Set-Cookie: atlassian.xsrf.token=BGEA-KKM0-ZLFV-1RQ2|db9e7165e3e35aff332231f690acea456d61fe25|lin;path=/
curlStr=curl -D- -u user:pwd -X POST --data "{\"fields\":{\"project\":{\"key\":\"WW\"},\"summary\":\"Summary not set.\",\"description\":\"Creating of an issue using project keys and issue type names using the REST API.\",\"issuetype\":{\"name\":\"Issue name\"},\"customfield_14702\":[{\"key\":\"SM-480070\"}],\"customfield_14200\":[{\"key\":\"SM-255197\"}],\"customfield_14700\":[{\"key\":\"SM-255088\"}],\"customfield_14701\":[{\"key\":\"SM-255202\"}],\"customfield_14706\":\"2019-06-13T12:57:00.00+0200\",\"customfield_10403\":\"2019-06-13T13:47:59.130+0200\"}}" -H "Content-Type: application/json" http://some.server.de/rest/api/2/issue/
请注意,我更改了用户名和密码之类的内容以保护敏感数据。
我期望在命令或常规解释某些与我期望不同的东西时避免转义某些字符。如何正确执行Groovy中的命令?