Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying Http headers in Elm

Tags:

http

elm

My Elm program works fine with the code (excerpt) below using http.get, but I had to changed it to a custom request to specify JWT in the header, and I get the following error due to type mismatch.

I think I need to change the type of request to Http.Request (List QFields) but not sure how to. Apparently, I can't make it like { verb = "Get" ...} decoder because { verb ... } is not a function.

The 2nd argument to function `send` is causing a mismatch.

264|                Http.send FetchHNTopStories request
                                                ^^^^^^^
Function `send` is expecting the 2nd argument to be:

    Http.Request (List QFields)

But it is:

    Request

<Working code>

request : Http.Request (List QFields)
request = 
    let 
       decoder =
        JD.at [ "data", "qQry" ] <|
            JD.list qDecoder
    in
       Http.get ("http://localhost:3000/graphql?query=" ++ encoded) decoder

type Msg
    = SendMessage
    | FetchHNTopStories (Result Http.Error (List QFields))
     ...

initModel : Taco -> ( Model, Cmd Msg )
initModel taco =
    let
        startModel = { newMessage = ""
                    }

        cmd =  Http.send FetchHNTopStories request  
    in
        ( startModel
           ! [cmd]
        )

<Changed code - Not working>

request : Request
request  =
    let

        decoder =
            JD.at [ "data", "Qry" ] <|
                JD.list qDecoder

        headers= [
            ("Authorization","Bearer eyJhbGciOiJIUzUxM...kv6TGw7H1GX2g")
        ]

    in
          { verb = "GET"
            , headers = headers
            , url = url
            , body = Http.emptyBody
            , expect = Http.expectJson decoder
            }  
like image 706
Jason O. Avatar asked Apr 07 '17 07:04

Jason O.


People also ask

How do I set HTTP headers?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

Can HTTP headers be custom?

There are many uses for custom headers and they are quite commonly used. Even if you aren't using a CDN or haven't specifically defined any custom HTTP headers on your origin server, you may still be sending responses with custom headers.

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.


1 Answers

If I understand correctly, you need to use Http.request, instead of Http.get, and supply it a valid record, like this:

request : Http.Request (List QFields)
request =
    let
        decoder =
            JD.at [ "data", "Qry" ] <|
                JD.list qDecoder

        headers =
            [ ( "Authorization", "Bearer eyJhbGciOiJIUzUxM...kv6TGw7H1GX2g" )
            ]
    in
    Http.request -- This line is missing from your code
        { method = "GET"
        , headers = headers
        , url = url
        , body = Http.emptyBody
        , expect = Http.expectJson decoder
        , timeout = Nothing
        , withCredentials = False
        }  
like image 147
ahstro Avatar answered Sep 20 '22 02:09

ahstro