0

I use rest API on Ruby with sinatra. I got payment information from IAMPORT,,,

def get_authrestapi()
    @key = IMP_KEY
    @secret = IMP_SECRET

    response = RestClient.post 'https://api.iamport.kr/users/getToken', {'imp_key' => @key, 'imp_secret' => @secret}, :accept => :json

    json = JSON.parse(response.to_json, symbolize_names: true)

    return json['response']['access_token']
end

but, I got error message... like below

JSON::ParserError at /payments 757: unexpected token at '"{\"code\":0,\"message\":null,\"response\":{\"access_token\":\"9898....", "..."}}"'

How can I solve this problem?? I think,, there is problem that variable 'json' is not HASH..

Thanks.

Younghun Jung
  • 229
  • 1
  • 13

1 Answers1

1

Don't convert the response to json. It's already json.

Replace the following line:

json = JSON.parse(response.to_json, symbolize_names: true)

with:

json = JSON.parse(response, symbolize_names: true)
falsetru
  • 314,667
  • 49
  • 610
  • 551
  • @Зелёный, forgot the response is string-like object. removed the `to_s`. Thank you for the feedback. – falsetru Nov 20 '16 at 09:11
  • response according to the documentation it is an object but https://github.com/rest-client/rest-client#usage-raw-url `response.body` return a string I'am little bit confused. But your answer is right. – Зелёный Nov 20 '16 at 09:13
  • @Зелёный, According to https://github.com/rest-client/rest-client#result-handling, New in 2.0: RestClient::Response objects are now a subclass of String. Previously, they were a real String object with response functionality mixed in, which was very confusing to work with. – falsetru Nov 20 '16 at 09:16
  • @falsetru I replace line: json = JSON.parse(response.to_json, symbolize_names: true) with json = JSON.parse(response, symbolize_names: true) However, I got a error message like this: NoMethodError at /payments undefined method `[]' for nil:NilClass I think there is problem in this part : return json['response']['access_token'] – Younghun Jung Nov 21 '16 at 02:00
  • In previous error, I can check string obj: "'{\"code\":0,\"message\":null,\"response\":{\"access_token\‌​":\"9898....", "..."}}"' so, response has payment information, right. but, I cannot access json with hash data structure after JSON.parse(...). Obviously, there is 'response' key including 'access_token' key and value... It's so hard thing.. – Younghun Jung Nov 21 '16 at 02:14
  • @YounghunJung, Could you share the output of `puts response.to_s` as is (only masking confidential information) – falsetru Nov 21 '16 at 04:46
  • @falsetru, I search other questions about accessing irb in sinatra app. For checking the output of response.to_s, Do I install racksh gem to use sinatra shell like console? – Younghun Jung Nov 21 '16 at 08:05
  • @YounghunJung, `IO::write('/tmp/json', response.to_s)` to write the response body to a file. Then show me the file content (mask only confidential information) – falsetru Nov 21 '16 at 13:33
  • @falsetru, hi I got this value through puts response : {"code":0,"message":null,"response":{"access_token":"b98d2618d2916f695098aa6da0ba93d4396b0256","now":1480335206,"expired_at":1480335861}} After, json = JSON.parse(response,:symbolize_names => true), return json['response']['access_token'],,, There is still error message,, undefined method `[]' for nil:NilClass – Younghun Jung Nov 28 '16 at 12:15