首页 > 解决方案 > 使用 groovy-wslite 从 CURL 请求发出 post 请求

问题描述

嗨,我有以下 CURL 请求,我想使用 groovy-wslite 库将其包含在 groovy 脚本中,以使请求正常工作。

curl -s -X POST -k -u user:password https://artifactory_url/api/search/aql -H 'Content-Type: text/plain' -d 'items.find({"type":"file","repo":{"$eq": "my-repo-name"},"path":{"$match":"com/mycompany/product1/subcat/mob/*"},"name":{"$match":"*apk"}}).sort({"$desc":["path"]}).limit(1)'

标签: restcurlgroovy

解决方案


您可以使用http-builder-ng并且您的代码看起来像

compile 'io.github.http-builder-ng:http-builder-ng-CLIENT:1.0.4'
HttpBuilder.configure {
    request.uri = 'https://artifactory_url/api/search/aql'
    request.auth.basic 'un', 'pw'
    request.contentType = 'text/plain'
}.post {
    request.body = 'items.find({"type":"file","repo":{"$eq": "my-repo-name"},"path":{"$match":"com/mycompany/product1/subcat/mob/*"},"name":{"$match":"*apk"}}).sort({"$desc":["path"]}).limit(1)'

    response.success { FromServer fs, Object body ->
        println body
    }
}

推荐阅读