Younghun Jung
Younghun Jung

Reputation: 361

Ruby on sinatra, JSON::parserError issue and 757: unexpected token

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.

Upvotes: 0

Views: 981

Answers (1)

falsetru
falsetru

Reputation: 369054

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)

Upvotes: 1

Related Questions