在 ruby 中使用 sinatra,如何在 rest 客户端上使用 get 方法获取响应?

In ruby with sinatra, How to get I response with get method on rest client?

我将 ruby 与 sinatra 一起使用,并在导入时使用 rest-client 进行付款。 我得到了通过特定 url 上的 post 方法键入的字符串的标记:'... /users/getToken'。 使用此令牌,我想通过此 url:

上的 get 方法获取付款信息

get_url = 'https://api/iamport.kr/payments/'+imp_uid

详细代码如下,

def get_paymentsdetails(token, imp_uid)
    get_url = 'https://api.iamport.kr/payments/'+imp_uid

    response = RestClient.get get_url, :data => {}.to_json, :accept => :json, :headers => {'Authorization' => token}
    json = JSON.parse(response, :symbolize_names => true)
    # json = JSON.parse(response.to_json, {:symbolize_names => true})

    return json
end

但是,我在这部分代码上遇到了 401 未经授权的错误。

response = RestClient.get get_url, :data => {}.to_json, :accept => :json, :headers => {'Authorization' => token}

在我使用特定的 imp_uid 访问 get_url 之后,我得到了这个页面,{"code":-1,"message":"Unauthorized","response":null}

我检查了 get_paymentsdetails 函数的参数标记和 imp_uid 具有有效的字符串值,那么我如何访问响应参数? 我认为 response = RestClient.get get_url..... code.

有一些问题

谢谢。

方法'get'来自'RestClient'classreturn一些具有属性的对象。所以响应没有什么价值。您需要其中的哪一个?您可以通过他们的名字访问他们,其描述为 here

在你的情况下,在 response = RestClient.get get_url... 之后你应该有变量 response 和调用 response.headers, response.coderesponse.body.
[=22= 的能力] 但是我担心你在授权方面有一些问题,这意味着 imp_uidtoken 是不正确的。这就是为什么远程服务器发送给您的响应是 http 代码 401(未授权)。如果是这样,您应该尝试检查您的 imp_uidtoken。如果一切正确,请尝试获得 iamport.kr 的支持。