NetworkError for a simple HTTP 404?

Hi, folks. I’m rediscovering Elm. I’m adapting the “Book” example to display the details of the Http.Error on error responses. I was very surprised to discover that when I try to request text from this URL, I get NetworkError instead of BadStatus 404.

https://elm-lang.org/assets/public-opinion-this-does-not-exist.txt

To be clear, I changed only the URL in the init function:

init : () -> ( Model, Cmd Msg )
init _ =
    ( Loading
    , Http.get
        { url = "https://elm-lang.org/assets/public-opinion-this-does-not-exist.txt"
        , expect = Http.expectString FetchPublicOpinionResponse
        }
    )

As far as I can tell, that’s a perfectly cromulent URL. Both Firefox and cURL answer with HTTP Status 404.

Is there a bug or is the problem coming from inside the brain?

Thanks.

Hi!

From my limited testing (see https://ellie-app.com/pH4j93hrLqMa1, the request is done when the application starts), it seems that this URL responds with a restrictive access-control-allow-origin https://worker.elm-lang.org, which means any request done from JavaScript is going to get rejected, unless it’s initiated by code living at https://worker.elm-lang.org.

This is likely why you’re seeing a NetworkError. You’re seeing a regular 404 when you make that request from more traditional means (direct browser request or curl) because CORS enforcement does not work the same in those cases.

Hope that helps!

Thank you! Based on your description, I’d expect even a successful request to be rejected due to CORS, but the text loads when the URL is correct. I admit, I don’t understand CORS yet. :person_shrugging:

Huh. It seems to be something about the server that’s serving these URLs. When I request the document at the correct URL, I see

access-control-allow-origin: *

and when I request a URL that almost certainly doesn’t have a document on the server, I see

access-control-allow-origin: https://worker.elm-lang.org

I guess the server is doing something sensible: explicitly allowing CORS for the document it knows it has and denying it otherwise.

Did I get it? :person_shrugging:

I don’t know for sure but your understanding of the situation makes sense to me =)

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.