Выполнение команды cURL в Groovy правильно - PullRequest
0 голосов
/ 13 июня 2019

Я хочу использовать команду cURL для отправки данных JSON на сервер через POST. Это для создания проблемы 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?

...