Why does my Julia server on localhost hang?

I'm trying to start a simple server in Julia. (Julia 1.7, HTTP v0.9.16, Windows 10).

By copying the snippet in the HTTP docs, I've written

using HTTP
using Sockets
HTTP.serve(Sockets.localhost, 8000) do request::HTTP.Request 
    @show HTTP.header(request, "Content-Type")
    @show HTTP.payload(request)
    return HTTP.Response("Helllllo")

in a julia terminal.

When i navigate to http://localhost:8000/ in Chrome, it Does show a page with the word "Helllllo".

But, when i try to send GET requests to it, it doesn't answer. Things I tried:

  • Opening a new julia terminal and writing

     using HTTP
     r = HTTP.request("GET", "");

What happens is that this command hangs, instead of producing a r.

  • Going in Chrome console and writing

       .then(res => res.json())

Again, no answer.

What am I doing wrong?


1 answer

  • answered 2021-11-28 15:46 Sundar R

    A couple different issues:

    1. You're trying to read from a https address, after having set up a plain http server. Once you fix this, Julia is able to get a request-response:
    julia> r = HTTP.request("GET", "")
    HTTP/1.1 200 OK
    Transfer-Encoding: chunked
    1. But it still fails in the browser (in Firefox - I assume it will be the same in Chrome) with

    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at (Reason: CORS request did not succeed).

    This is simply because != localhost for browser, for security purposes (so it doesn't allow you to read from the "different" host at while you're on localhost). If you instead do:

    >> fetch("http://localhost:8000")
    Promise { <state>: "pending" }
    Response { type: "basic", url: "http://localhost:8000/", redirected: false, status: 200, ok: true, statusText: "OK", headers: Headers, body: ReadableStream, bodyUsed: false }

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum