# Reading images in HTTP doesn't seem to terminate

**URL:** <https://discourse.julialang.org/t/reading-images-in-http-doesnt-seem-to-terminate/47059>\
**Category:** New to Julia\
**Created:** [September 22, 2020, 11:19am UTC](https://discourse.julialang.org/t/reading-images-in-http-doesnt-seem-to-terminate/47059 "2020-09-22T11:19:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 22, 2020, 11:19am UTC](https://discourse.julialang.org/t/reading-images-in-http-doesnt-seem-to-terminate/47059/1 "2020-09-22T11:19:11Z")

</div>

```julia
using HTTP

using Colors

# initialize to nothing
orig_img = nothing

using ImageMagick
HTTP.listen() do http::HTTP.Stream
    global orig_img
    @show http.message
    @show HTTP.header(http, "Content-Type")
    while !eof(http)
        orig_img = ImageMagick.readblob( readavailable(http))
    end
end

orig_img

```

and then I curl an image to it by

```julia
curl -X POST --data-binary @"path/to/some.png" 127.0.0.1:8081

```

But the above the doesn’t seem to terminate even for a small image.

When I check orig\_img it looks like the image got upload if a bit grainy.

But when I terminate curl everything dies with

```julia
julia> ┌ Error: (Base.IOError("read: connection reset by peer (ECONNRESET)", -4077), Base.StackTraces.StackFrame[wait_readnb(::Sockets.TCPSocket, ::Int64) at stream.jl:350, eof at stream.jl:48 [inlined], read_to_buffer(::HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, ::Int64) at ConnectionPool.jl:251, readuntil(::HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, ::Function, ::Int64) at ConnectionPool.jl:271, readuntil at ConnectionPool.jl:269 [inlined], readheaders at Messages.jl:471 [inlined], startread(::HTTP.Streams.Stream{HTTP.Messages.Request,HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}) at Streams.jl:155, handle_transaction(::var"#19#20", ::HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, ::HTTP.Servers.Server{Nothing,Sockets.TCPServer}; final_transaction::Bool) at Servers.jl:343, (::HTTP.Servers.var"#handle_transaction##kw")(::NamedTuple{(:final_transaction,),Tuple{Bool}}, ::typeof(HTTP.Servers.handle_transaction), ::Function, ::HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, ::HTTP.Servers.Server{Nothing,Sockets.TCPServer}) at Servers.jl:338, handle_connection(::Function, ::HTTP.ConnectionPool.Connection{Sockets.TCPSocket}, ::HTTP.Servers.Server{Nothing,Sockets.TCPServer}, ::Int64, ::Int64) at Servers.jl:299, (::HTTP.Servers.var"#8#9"{var"#19#20",HTTP.Servers.Server{Nothing,Sockets.TCPServer},Base.RefValue{Int64},Int64,Int64,Bool,HTTP.ConnectionPool.Connection{Sockets.TCPSocket}})() at task.jl:356])

```

I suspect I am just not understanding curl because I thought the image would have finished uploading and it will terminate.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 22, 2020, 11:27am UTC](https://discourse.julialang.org/t/reading-images-in-http-doesnt-seem-to-terminate/47059/2 "2020-09-22T11:27:15Z")

</div>

I think I get it. I need to send a response like this so it will terminate

```julia
using HTTP

using Colors

# initialize to nothing
orig_img = nothing

using ImageMagick
HTTP.listen() do http::HTTP.Stream
    global orig_img
    @show http.message
    @show HTTP.header(http, "Content-Type")
    while !eof(http)
        orig_img = ImageMagick.readblob( readavailable(http))
    end

    HTTP.setstatus(http, 404)
    HTTP.setheader(http, "Foo-Header" => "bar")
    HTTP.startwrite(http)
    write(http, "response body")
    write(http, "more response body")
end

orig_img

```

---

<div class="post-metadata">

**Author:** ![Eric\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_smith/32/8088_2.png) [@Eric\_Smith](https://discourse.julialang.org/u/Eric_Smith)\
**Post date:** [September 22, 2020, 2:10pm UTC](https://discourse.julialang.org/t/reading-images-in-http-doesnt-seem-to-terminate/47059/3 "2020-09-22T14:10:57Z")

</div>

Super 💪🙏 @xiaodai

I have all working and now with HTTP.jl with required architectural changes to the app.  
It is CD/CI deployed to a container in Azure which is served via a Web App.

But now stumbling on defining the `port` for HTTP.jl to listen on.  
Getting errors with below to examples following what docs I could cut and paste from:

`HTTP.listen(host=Sockets.localhost, port=8081) do http`

> ERROR: InitError: UndefVarError: Sockets not defined

`HTTP.listen(, port=80) do http`

> ERROR: LoadError: syntax: unexpected “,”

I am specifying the default port here but the purpose is to change to port `80` for production.  
Surely, I am missing something obvious.

Any ideas welcome.  
Eric

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 22, 2020, 2:19pm UTC](https://discourse.julialang.org/t/reading-images-in-http-doesnt-seem-to-terminate/47059/4 "2020-09-22T14:19:31Z")

</div>

> [@Eric\_Smith](#):
>
> Sockets not defined

`using Sockets`

> [@Eric\_Smith](#):
>
> HTTP.listen(, port=80)

this is not valid syntax in Julia, unlike R.

---

<div class="post-metadata">

**Author:** ![Eric\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_smith/32/8088_2.png) [@Eric\_Smith](https://discourse.julialang.org/u/Eric_Smith)\
**Post date:** [September 22, 2020, 2:25pm UTC](https://discourse.julialang.org/t/reading-images-in-http-doesnt-seem-to-terminate/47059/5 "2020-09-22T14:25:26Z")

</div>

Thanks again @xiaodai .

I think that one is good - not sure why it works now. Testing on full production.  
`HTTP.listen("127.0.0.1", 80) do http"`  
Getting there.
