【问题标题】:Specifying Http headers in Elm在 Elm 中指定 Http 标头
【发布时间】:2017-09-02 11:24:35
【问题描述】:

我的 Elm 程序在下面使用 http.get 的代码(摘录)上运行良好,但我不得不将其更改为自定义请求以在标头中指定 JWT,并且由于类型不匹配而出现以下错误。

我想我需要将request 的类型更改为Http.Request (List QFields),但不知道该怎么做。显然,我不能让它像{ verb = "Get" ...} decoder,因为{ verb ... } 不是一个函数。

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
            }  

【问题讨论】:

    标签: http elm


    【解决方案1】:

    如果我理解正确,您需要使用Http.request,而不是Http.get,并为其提供有效记录,如下所示:

    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
            }  
    

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 2020-04-01
      • 1970-01-01
      • 2011-11-20
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 2017-06-22
      相关资源
      最近更新 更多