简体   繁体   English

带有空体的 Http DELETE

[英]Http DELETE with empty body

In Elm (0.18) I'm calling one http DELETE endpoint that if successful responds with 200 and an empty body.在 Elm (0.18) 中,我正在调用一个 http DELETE 端点,如果成功则以 200 和一个空主体进行响应。

In this case (of success) I need to pass back a message with the initial id ( OnDelete playerId ).在这种情况下(成功),我需要用初始 id ( OnDelete playerId ) 传回一条消息。 But as the body is empty i can't parse it from there.但是由于身体是空的,我无法从那里解析它。

Currently I'm doing it like this, but is there a more elegant way to write the expect part of Http.Request :目前我正在这样做,但是有没有更优雅的方式来编写Http.Requestexpect部分:

Http.expectStringResponse (\response -> Ok playerId)

? ?

This reflects my current code:这反映了我当前的代码:

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 added expectWhatever . Elm v0.19 添加了expectWhatever It behaves slightly different with the Result being checked for errors, but a similar effect.它的行为与检查错误的Result略有不同,但效果相似。


I've created a helper expectUnit for "empty" 200 responses.我为“空”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
        }

But for you, the best you could get is.但对你来说,你能得到的最好的就是。

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

Or you could create a helper (which is actually the singleton or pure for Expect )或者您可以创建一个助手(实际上是Expectsingletonpure

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

Which could be used like可以像这样使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM