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

Uncaught TypeError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': Value is not a valid ByteString #70

Open
decioferreira opened this issue May 1, 2020 · 0 comments

Comments

@decioferreira
Copy link

decioferreira commented May 1, 2020

SSCCE

module Main exposing (main)

import Browser
import Html exposing (text)
import Http


main : Program () () ()
main =
    Browser.element
        { init =
            \_ ->
                ( ()
                , Http.request
                    { method = "GET"
                    , headers = [ Http.header "foo" "âçéÿ€èëïîôœæâà" ]
                    , url = "https://www.example.com"
                    , body = Http.emptyBody
                    , expect = Http.expectWhatever (always ())
                    , timeout = Nothing
                    , tracker = Nothing
                    }
                )
        , view = \_ -> text ""
        , update = \_ model -> ( model, Cmd.none )
        , subscriptions = \_ -> Sub.none
        }

Link to ellie: https://ellie-app.com/8KGdFZz6j7qa1

Problem

When adding a HTTP header value with non UTF-8 characters we see the following error:

Uncaught TypeError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': Value is not a valid ByteString

HTTP headers can only contain UTF-8 Strings (see ByteString).

This might relate to #53.

Workaround

Use Url.percentEncode to encode the value.

module Main exposing (main)

import Browser
import Html exposing (text)
import Http
import Url


main : Program () () ()
main =
    Browser.element
        { init =
            \_ ->
                ( ()
                , Http.request
                    { method = "GET"
                    , headers = [ Http.header "foo" (Url.percentEncode "âçéÿ€èëïîôœæâà") ]
                    , url = "https://www.example.com"
                    , body = Http.emptyBody
                    , expect = Http.expectWhatever (always ())
                    , timeout = Nothing
                    , tracker = Nothing
                    }
                )
        , view = \_ -> text ""
        , update = \_ model -> ( model, Cmd.none )
        , subscriptions = \_ -> Sub.none
        }

Link to ellie: https://ellie-app.com/8KJ2mmzRZgPa1

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