我的 Elm 程序使用 http.get
可以很好地处理下面的代码(摘录),但我不得不将其更改为自定义请求以在 header 中指定 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.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
}
关于http - 在 Elm 中指定 Http header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43271974/