0

I request information from an HTTP streaming service. It provides data in JSON format. Here is the documentation. Here is a part of the code I am using:

require 'uri'
require 'net/https'
require 'json'


uri = URI("https://api.tradier.com/v1/markets/events/session")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
http.read_timeout = 30
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

# Headers

request["Accept"] = "application/json"
request["Authorization"] = "Bearer xxx"

# Send synchronously

response = http.request(request)

# parses response

parse = JSON.parse(response.body)

#out puts values only from response

sessionid = parse.values[0]["sessionid"]
url = parse.values[0]["url"]


uri = URI("#{url}?sessionid=#{sessionid}&symbols=aapl")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
http.read_timeout = 30
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

# Headers

request["Accept"] = "application/json"
request["Authorization"] = "Bearer xxx"


http.request request do |response|
  response.read_body do |data|
    puts data.class
  #  info = JSON.parse(data, :quirks_mode => true)
  #  puts info.values
    end
end

I want to have the system continue the program. It seems that I need to use begin and rescue, but I cannot get them to work. When I request data, I get the following error:

`parse': 757: unexpected token at '{"type":"trade","symbol":"AAPL","exch":"Q","price":"191.23","size":"1081622","cvol":"18308460","date":"1528747200000","last":"191.23"}{"type":"summary","symbol":"AAPL","open":"191.35","high":"191.97","low":"190.21","prevClose":"191.7","close":"191.23"}' (JSON::ParserError)

8
  • Please share how do you build a request and what the value of response is. Commented Jun 12, 2018 at 3:17
  • depending on your library it may have already parsed the JSON into a hash; do a puts data.class before info = JSON... and check it out Commented Jun 12, 2018 at 3:21
  • Hey Josh, I went ahead and did that and it spit out "string" Commented Jun 12, 2018 at 3:55
  • I have added the whole script. I first have to get a session ID and then use that to open the string. Commented Jun 12, 2018 at 3:57
  • 1
    Can you post the data string that's actually generating the error? Or it is the full string between ' ' in the yellow quote? In that case there seems to be a strange }{ Commented Jun 12, 2018 at 5:52

1 Answer 1

2

The endpoint you are using is documented in Tradier API docs and it's a streaming endpoint.

It appears that the response is not chunked to contain just one JSON document per chunk. It however does appear that the documents are separated by linefeeds, making the response look like:

{ "json": "data" }
{ "more": "data" }

And that is not valid JSON. You probably need to parse them one by one by doing something like:

http.request request do |response|
  response.read_body do |data|
    data.each_line do |chunk|
      info = JSON.parse(chunk)
      puts info.inspect
    end
  end
end

If the response chunking happens in the middle of JSON documents, you must use some kind of buffered reader.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this information. I tried this solution, however it is still producing an error for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.