gpt4 book ai didi

带有空主体的 Http DELETE

转载 作者:可可西里 更新时间:2023-11-01 16:07:30 30 4
gpt4 key购买 nike

在 Elm (0.18) 中,我正在调用一个 http DELETE 端点,如果成功,它会以 200 和一个空主体响应。

在这种情况下(成功)我需要传回一 strip 有初始 ID (OnDelete playerId) 的消息。但由于 body 是空的,我无法从那里解析它。

目前我是这样做的,但是有没有更优雅的方式来编写 Http.Requestexpect 部分:

Http.expectStringResponse (\response -> Ok playerId)

?

这反射(reflect)了我当前的代码:

deletePlayer : PlayerId -> Cmd Msg
deletePlayer playerId =
deleteRequest playerId
|> Http.send OnDelete


deleteRequest : PlayerId -> Http.Request PlayerId
deleteRequest playerId =
Http.request
{ body = Http.emptyBody

, expect = Http.expectStringResponse (\response -> Ok playerId)

, headers = []
, method = "DELETE"
, timeout = Nothing
, url = "http://someHost/players/" ++ playerId
, withCredentials = False
}


type alias PlayerId =
String

最佳答案

添加了 Elm v0.19 expectWhatever .它的行为与检查错误的 Result 略有不同,但效果相似。


我为“空”200 响应创建了一个助手 expectUnit

expectUnit : Expect ()
expectUnit =
Http.expectStringResponse << always <| Ok ()



deleteThing : String -> Request ()
deleteThing path =
Http.request
{ method = "DELETE"
, headers = []
, url = "http://localhost/api"
, body = Http.jsonBody <| Encode.object [ ( "path", Encode.string path ) ]
, expect = expectUnit
, timeout = Nothing
, withCredentials = False
}

但对你来说,你能得到的最好的是。

{ ...
, expect = Http.expectStringResponse << always <| Ok playerId
...
}

或者您可以创建一个助手(实际上是 Expectsingletonpure)

alwaysExpect : a -> Expect a
alwaysExpect =
Http.expectStringResponse << always << Ok

可以像这样使用

{ ...
, expect = alwaysExpect playerId
...
}

关于带有空主体的 Http DELETE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41884524/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com