# Switch from HttpServer.jl to HTTP.jl

**URL:** <https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717>\
**Category:** Internals & Design\
**Tags:** question, package\
**Created:** [January 16, 2019, 7:23pm UTC](https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717 "2019-01-16T19:23:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![NiharikaPatel](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@NiharikaPatel](https://discourse.julialang.org/u/NiharikaPatel)\
**Post date:** [January 16, 2019, 7:23pm UTC](https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717/1 "2019-01-16T19:23:37Z")

</div>

Hi,

I recently upgraded to Julia 7 and one of the HttpServer.jl package is not compatible anymore. Im trying to switch to HTTP.jl instead but I am very new to this concept.  
This is the how the current code looks:

function get\_http\_handler()  
return HttpHandler() do request::Request, response::Response  
log(“Request”)

```
    headers = get_headers()
    output = nothing
    log(request.resource)
    log(request.method)
    if request.method == "OPTIONS"
        return Response(200, headers)
    elseif request.resource == "/run" && request.method == "POST"
        inputs_string = String(request.data)
        output = run_simulation(inputs_string)

```

end  
response\_body = JSON.json(output)  
log(“Response body:”)  
response = Response(response\_body, headers)  
log(“Response:”, response)  
return response  
end

function run\_web\_server()  
http\_handler = get\_http\_handler()  
server = Server(http\_handler)  
run(server, 8080)  
http.events[“error”] = (client, err) → begin  
server = Server(http\_handler)  
run(server, 8080)  
end  
end

run\_web\_server()

What do I have to change to make it work with HTTP.jl ? Since, I am very new to this, I dint find the documentation as straight forward as i would like. Any help would be appreciated.

Thnaks

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [January 16, 2019, 11:22pm UTC](https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717/2 "2019-01-16T23:22:37Z")

</div>

Before we can adequately assist you, would you mind please reading this post and then updating your post:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

Specifically the parts about creating a “Minimum Working Example” (what does `run_simulation()` do, I don’t see it defined in your post), and using triple-backticks properly.

---

<div class="post-metadata">

**Author:** ![NiharikaPatel](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@NiharikaPatel](https://discourse.julialang.org/u/NiharikaPatel)\
**Post date:** [January 16, 2019, 11:39pm UTC](https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717/3 "2019-01-16T23:39:15Z")

</div>

Thank you for the recommendation.  
Please consider the updated code.

```julia
function get_http_handler()
return HttpHandler() do request::Request, response::Response
log(“Request”)
    headers = get_headers()
    output = nothing
    log(request.resource)
    log(request.method)
    if request.method == "OPTIONS"
        return Response(200, headers)
    elseif request.resource == "/run" && request.method == "POST"
        inputs_string = String(request.data)
        output = run_simulation(inputs_string)
   end
   response_body = JSON.json(output)
   log(“Response body:”)
  response = Response(response_body, headers)
  log(“Response:”, response)
  return response
end

```

```julia
function run_web_server()
  http_handler = get_http_handler()
  server = Server(http_handler)
  run(server, 8080)
  http.events["error"] = (client, err) -> begin
  server = Server(http_handler)
  run(server, 8080)
  end
end

run_web_server()

```

The run\_simulation function is what calls our tool at the back end. It returns a simulation ID which is String as a response.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [January 16, 2019, 11:53pm UTC](https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717/4 "2019-01-16T23:53:59Z")

</div>

Re-pasting this and cleaning up a bit. You have some bad quotation marks that don’t parse, and a missing end statement:

```julia
using HTTP, JSON

function get_http_handler()
    return HttpHandler() do request::Request, response::Response
        log("Request")
        headers = get_headers()
        output = nothing
        log(request.resource)
        log(request.method)
        if request.method == "OPTIONS"
            return Response(200, headers)
        elseif request.resource == "/run" && request.method == "POST"
            inputs_string = String(request.data)
            output = run_simulation(inputs_string)
        end
        response_body = JSON.json(output)
        log("Response body:")
        response = Response(response_body, headers)
        log("Response:", response)
        return response
    end
end

function run_web_server()
    http_handler = get_http_handler()
    server = Server(http_handler)
    run(server, 8080)
    http.events["error"] = (client, err) -> begin
        server = Server(http_handler)
        run(server, 8080)
    end
end

run_web_server()

```

Given that going from `HttpServer.jl` to `HTTP.jl` is not a simple changed `using` statement, I would recommend you read the `HTTP.jl` docs ([Home · HTTP.jl](https://juliaweb.github.io/HTTP.jl/stable/)) and try to put things together piece-by-piece. If I (or anyone else) gets some free time soon, I’ll try to figure it out on my own or provide some additional hints. However, the best way to fix this is to try translating it on your own, with the help of the docs and examples in the repository. That way you’ll know how to maintain and update it in the future too 😄

---

<div class="post-metadata">

**Author:** ![NiharikaPatel](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@NiharikaPatel](https://discourse.julialang.org/u/NiharikaPatel)\
**Post date:** [January 17, 2019, 12:21am UTC](https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717/5 "2019-01-17T00:21:06Z")

</div>

Sorry, its my first time on Discourse and thank you for the guidelines. If there is a simple example on setting up a webserver that receives requests and sends out responds please do share. I will go through the documentation as well to understand it better.  
Thank you

---

<div class="post-metadata">

**Author:** ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)\
**Post date:** [January 17, 2019, 2:51am UTC](https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717/6 "2019-01-17T02:51:27Z")

</div>

Here’s an example using [Bukdu](https://github.com/wookay/Bukdu.jl), an web server based on HTTP.jl

```julia
# https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717

using Bukdu
using HTTP.Messages: setheader

struct SimulationController <: ApplicationController
    conn::Conn
end

function take_options(c::SimulationController)
    req = c.conn.request
    @info :req_headers req.headers
    @info :req_method_target (req.method, req.target)
    # setheader(req.response, "Access-Control-Allow-Origin" => "*")
    setheader(req.response, "Content-Length" => "0")
    nothing
end

function run_simulation(inputs_string::String)
    @info :inputs inputs_string
    # work
end

function run_simulation(c::SimulationController)
    inputs_string = String(c.conn.request.body)
    output = run_simulation(inputs_string)
    render(JSON, output)
end

routes() do
    Bukdu.options("/", SimulationController, take_options)
    Bukdu.options("/run", SimulationController, take_options)
    post("/run", SimulationController, run_simulation)
end

Bukdu.start(8080)

#=
curl -i -X OPTIONS http://localhost:8080/
curl -i -X OPTIONS http://localhost:8080/run
curl -i -H "Content-Type: text/plain" -d "data" http://localhost:8080/run
=#

```

---

<div class="post-metadata">

**Author:** ![NiharikaPatel](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@NiharikaPatel](https://discourse.julialang.org/u/NiharikaPatel)\
**Post date:** [January 17, 2019, 8:05pm UTC](https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717/7 "2019-01-17T20:05:29Z")

</div>

Thank you, I will check it out. In the future we plan on having an asynch web server. Can I do that using Bukdu? We mainly wanted to switch to HTTP.jl because it would give us more functionality moving forward.
