2017-02-14 1 views
2

rest_client에서 응답을 구문 분석하려고하면이 오류가 발생합니다.JSON :: ParserError : 743 : ROR의 예기치 않은 토큰

JSON :: ParserError : 743 :

require 'HTTParty' 
class Kele 
    include HTTParty 
    def initialize(email,password) 
     @options = {query: {email: email, password: password}} 
    end 

    def post 
     self.class.post('https://private-anon-8506c5665f-blocapi.apiary-mock.com/api/v1/sessions', @options) 
    end 
end 

I를 :에 예기치 않은 토큰 '{

루비 IRB에서
require 'rest_client' 
require 'json' 

class Kele 
    attr_accessor :data 
    def initialize(u,p) 
     #@values = {email: u, password: p} 
     @values = '{"email": "[email protected]", "password": "password"}' 
     @headers = {:content_type => 'application/json'} 
     @data = self.post 
    end 

    def post 
     response = RestClient.post 'https://private-anon-8506c5665f-blocapi.apiary-mock.com/api/v1/sessions', @values, @headers 
    end 
end 

r = b.post 
=> <RestClient::Response 200 "{\n \"auth..."> 

JSON.parse(r.body) 
=> JSON::ParserError: 743: unexpected token at '{ 

a = Kele.new(1,2) 
=> #<Kele:0x000000042e2e18 @values="{\"email\": \"[email protected]\", \"password\": \"password\"}", @headers={:content_type=>"application/json"}, @data=<RestClient::Response 200 "{\n \"auth...">> 

a.post.body 
=> "{\n \"auth_token\":\"eyJ0eXAiOiJKV1QiLCJhhGciOiJIUzI1NiJ9.eyJhcGlfa2V5IjoiYTc2MDZkNTBhYjA3NDE4ZWE4ZmU5NzliY2YxNTM1ZjAiLCJ1c2VyX2lkIjoyMzAzMTExLCJuYW1lIjoiQmVuIE5lZWx5In0.3VXD-FxOoxaGXHu6vmL8g191bl5F_oKe9qj8Khmp9F0\",\n \"user\":\n  {\n   \"id\":2307245,\n   \"email:\"[email protected]\",\n   \"created_at\":\"2015-08-11T16:31:08.836-07:00\",\n   \"updated_at\":\"2015-11-04T13:13:32.457-08:00\",\n   \"facebook_id\":null,\n   ...,\n   \"gender\":null\n  }\n}" 

은 나뿐만 아니라 HTTParty를 사용하여이 시도 여전히이 오류가 발생합니다 :

JSON.parse(a.post.body) 
=> JSON::ParserError: 743: unexpected token at '{ 

답변

1

두 번째 예에서 r은 JSON이 아니며 RestClient :: Response 개체이며 구문 분석 할 수 없습니다. 두 번째 예제에서 a.post.body으로 참조한 것처럼 RestClient :: Response의 r.body을 구문 분석해야합니다.

r = b.post   # => <RestClient::Response 200 "{\n \"auth..."> 
JSON.parse(r)  # => JSON::ParserError: ... 
r.body    # => "Some valid JSON string" 
JSON.parse(r.body) # => Parses "Some valid JSON string" 
+0

감사를 사용해야하지만, 나는 그것을 시도 않았고,이 같은 오류를 발생

. 방금 HTTParty를 사용하려고했는데 같은 오류가 발생했습니다. – Leogoesger

0

JSON의 743 오류는 모두 같지만 내 경우에는 API 끝점 때문에 발생합니다. 그것은 내가 사용하려고 생각하는 것과 약간 다릅니다.

이 오류가 발생하면 먼저 API 끝점 URL을 확인하고 올바른 것을 사용하고 있는지 확인하십시오. 이 경우

, 내가 https://www.bloc.io/api/v1/sessions

관련 문제