2017-01-26 45 views
3

在Elm(0.18)我打電話一個http DELETE端點,如果成功響應200和一個空的身體。Http刪除與空身體

在這種情況下(成功),我需要傳回一個消息與初始ID(OnDelete playerId)。但是由於身體是空的,我無法從那裏解析它。

目前我正在做這樣的,但在那裏寫的Http.Requestexpect部分更優雅的方式:

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 

回答

4

我創建了一個幫手expectUnit爲 「空」 200個迴應。

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 
... 
} 
+2

使用我喜歡,雖然它的alwaysExpect包裝方法讓我感到悲傷的是,沒有這種東西開箱。如果幾天之內沒有人會提供更好的答案,我會將其標記爲正確答案。 –