How to get and pass on json retrieved via HTTP

Hi, I am getting my feet wet in web-interactivity…so this might be a very basic question.

In the following code the retrieved messages are nicely printed out as a string containing the json information, but my JSON3.read does not work.

using HTTP, JSON3

io = Base.BufferStream()
ch=Channel{Dict}(10)
@async while !eof(io)
    bytes = readavailable(io)
    println(String(bytes))
    put!(ch,JSON3.read(String(bytes)))
end

@async HTTP.request("GET", "https://username:password@bluemix.cloudantnosqldb.appdomain.cloud/test/_changes?feed=continuous&since=now", response_stream=io)

I have also tried to make a version saving the string directly:

io = Base.BufferStream()
ch=Channel{String}(10)
@async while !eof(io)
    bytes = readavailable(io)
    println(String(bytes))
    put!(ch,String(bytes))
end

but this ends up only saving something like "" to ch, while the println still outputs the full json. I realise this may have less with webstack to do than conversion of types but I figured this is where people know how to do it best.

Its a bit hard to do a MWE as I don’t have an open couchDB server to set up at the moment…

Thanks for any pointers!

String is destructive, it empties your buffer. You can do something like

io = Base.BufferStream()
ch=Channel{Dict}(10)
@async while !eof(io)
    bytes = readavailable(io)
    text = String(bytes)    
    println(text)
    put!(ch, JSON3.read(text))
end

That’s it, thanks. Its these little details that take time to understand :-). Since I get an empty line in-between also I had to adjust as so also:

io = Base.BufferStream()
ch=Channel{Dict}(10)
@async while !eof(io)
    bytes = readavailable(io)
    text = String(bytes)    
    println(text)
    if length(text) > 0 && text[1] == '{'
        put!(ch, JSON3.read(text))
    end
end
1 Like

While length(text) > 0 is fine, but more common is !isempty(text). It can be more performant if length is costly and also it conveys better your intention.

1 Like

Changed! Thanks!