【问题标题】:how to use get method in groovyx.net.http.RESTClient properly如何正确使用 groovyx.net.http.RESTClient 中的 get 方法
【发布时间】:2015-06-04 20:53:12
【问题描述】:

我正在尝试通过 RESTClient 中的 get 方法获取 JSON 文件。

我正在尝试

def url = 'http://urlurlurl'
def username = 'username'
def password = 'password'
def restClient = new RESTClient(url)
restClient.auth.basic(username, password)

render restClient

当我看到我从 restClient 得到的,只是打印出来的

'groovyx.net.http.RESTClient@65333e2e'

这很难理解。

鉴于 url 是 API get 方法的端点,并且包含 JSON 文件,我如何检索 JSON 文件以便解析并使用该 JSON 文件?

我也在尝试这个

def url = 'http://urlurlurl'
def username = 'username'
def password = 'password'
def restClient = new RESTClient(url)
restClient.auth.basic(username, password)

//Adding get method
def jsonData = restClient.get(/* what value should I put in here?? */)

这给了我一个禁止的错误:

Error 500: Internal Server Error
URI: JsonRender
Class: groovyx.net.http.HttpResponseException
Message: Forbidden

有什么建议吗?在 RESTClient 中使用 get 方法的例子会很好。

【问题讨论】:

    标签: authentication grails plugins basic-authentication rest-client


    【解决方案1】:

    url 应该是您的 api 的基本 url。例如,如果我们想从完整 url 为 http://localhost:9200/user/app/_search 的 api 中搜索一些数据。因此,我们的基本 url 为http://localhost:9200/,api 的路径为user/app/_search。现在请求看起来像这样

    def client = new RESTClient( 'http://localhost:9200/' )
    def resp = client.get( path : 'user/app/_search')
    log.debug (resp.getContentAsString())
    

    希望这会成功。

    谢谢,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2019-09-12
      • 1970-01-01
      相关资源
      最近更新 更多