elm-http icon indicating copy to clipboard operation
elm-http copied to clipboard

High level requests for verbs other than POST/GET

Open langston-barrett opened this issue 8 years ago • 4 comments

Currently, there are only two "high-level" HTTP functions, get and post. It seems to me that PUT and DELETE would also be very useful. What do you think?

Parenthetically, the Consul HTTP API supports GET, PUT, and DELETE.

I'm currently using the following implementation of putString, delString:

{-| Mirror the getString API, but for PUT requests.                             
-}                                                                              
httpPutString : String -> String -> Task Error String                           
httpPutString =                                                                 
  wrapHttpApi "PUT"                                                             


{-| Mirror the getString API, but for DELETE requests.                          
-}                                                                              
httpDelString : String -> Task Error String                                     
httpDelString =                                                                 
  flip (wrapHttpApi "DELETE") ""                                                


{-| Create a high-level API for some HTTP verb other than GET/POST              
-}                                                                              
wrapHttpApi : String -> String -> String -> Task Error String                   
wrapHttpApi verb url body =                                                     
  let                                                                           
    request =                                                                   
      { verb = verb, headers = [], url = url, body = Http.string body }         
  in                                                                            
    Task.mapError promoteError (Http.send Http.defaultSettings request)         
      `andThen` handleResponse Task.succeed     

with handleResponse and promoteError copied directly from this module's source.

langston-barrett avatar Apr 14 '16 20:04 langston-barrett

The "PATCH" verb is also missing

skitoo avatar Apr 30 '16 09:04 skitoo

Here's a (full?) list of HTTP methods: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

I believe only the first three (GET, HEAD and POST) are widely implemented in browsers? Not sure if that means only those three make sense to have in the base API (and then only HEAD would be missing), or if it would make sense to have them all?

In any case, I'm not sure if it's relevant to have all those verbs implemented, or if something like the wrapHttpApi helper you posted above would be more useful?

magopian avatar Sep 05 '16 15:09 magopian

I do use the PUT request to send Trello API request from browser directly. Currently, I am using the snippet provided above for my scenario. I think Elm should provide such abilities and leave the question support or not support to the endpoint service.

lijunle avatar Sep 05 '16 18:09 lijunle

I use webmachine https://github.com/webmachine/webmachine for my backends, so yea having PUT and DELETE as first class things would be great

zkessin avatar Oct 09 '16 06:10 zkessin