Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime exception when empty header name is given #53

Open
ymtszw opened this issue Dec 9, 2018 · 0 comments
Open

Runtime exception when empty header name is given #53

ymtszw opened this issue Dec 9, 2018 · 0 comments

Comments

@ymtszw
Copy link

ymtszw commented Dec 9, 2018

Steps to reproduce:

  • Put header "" "something" in headers list for any Http request
module Main exposing (main)

import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
import Http


type alias Model =
    {}


init : () -> ( Model, Cmd Msg )
init _ =
    ( {}
    , Http.request
        { method = "GET"
        , headers = [ Http.header "" "something" ]
        , url = "https://www.example.com"
        , body = Http.emptyBody
        , expect = Http.expectWhatever (always NoOp)
        , timeout = Nothing
        , tracker = Nothing
        }
    )


type Msg
    = NoOp


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        NoOp ->
            ( model, Cmd.none )


view : Model -> Html Msg
view model =
    div [] []


main : Program () Model Msg
main =
    Browser.element
        { init = init
        , view = view
        , update = update
        , subscriptions = always Sub.none
        }

Result (runtime exception)

Uncaught SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': '' is not a valid HTTP header field name.

On the other hand, empty "value" does not cause exception.

Expected behavior/Proposal

What I actually wanted was, conditionally adding/not adding a Http.header based on a variable like so:

...
, headers =
    [ case fooBar of
        Foo str ->
            Http.header "foo" str
        Bar ->
            Http.header "" ""
    , ...
    ]
...

but as it turned out it ends up in a runtime exception.

  • At least we should not leak runtime exception, so Http.header "" "something" should either:
    • not compile
    • not try to setRequestHeader() at runtime (effectively none for header)

The latter could be useful since it can be used paired with if or case inside Lists, like I wanted to do.

As for my intent, for now we can just wrap it in a List for the same effect:

...
, headers =
    case fooBar of
        Foo str ->
            [ Http.header "foo" str ]
        Bar ->
            []
    , ...
    ]
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant