[英]Http DELETE with empty body
在 Elm (0.18) 中,我正在调用一个 http DELETE 端点,如果成功则以 200 和一个空主体进行响应。
在这种情况下(成功),我需要用初始 id ( OnDelete playerId
) 传回一条消息。 但是由于身体是空的,我无法从那里解析它。
目前我正在这样做,但是有没有更优雅的方式来编写Http.Request
的expect
部分:
Http.expectStringResponse (\response -> Ok playerId)
?
这反映了我当前的代码:
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
...
}
或者您可以创建一个助手(实际上是Expect
的singleton
或pure
)
alwaysExpect : a -> Expect a
alwaysExpect =
Http.expectStringResponse << always << Ok
可以像这样使用
{ ...
, expect = alwaysExpect playerId
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.