Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm, JSON decoder: How to decode an empty string?

Tags:

elm

What's the best way to handle an empty (no string at all) response?

Although the response code is 200, Elm returns an error because an empty response is not a valid JSON.

Here is my current code:

decodeAlwaysTrue : Json.Decode.Decoder Bool
decodeAlwaysTrue =
  Json.Decode.succeed True

Http.send Http.defaultSettings httpConfig
  |> Http.fromJson decodeAlwaysTrue
  |> Task.perform FetchFail DeleteUserSuccess

EDIT1:

This a POST action so I can't use getString.

like image 308
Guilhem Soulas Avatar asked Oct 19 '22 09:10

Guilhem Soulas


1 Answers

You could use the getString function from the Http module. That will give you back whatever string is returned from the HTTP request without attempting to convert is to a Json value.

If you instead need to use Http.send then you could do something like this:

Http.send Http.defaultSettings httpConfig
  |> Task.perform FetchFail (always DeleteUserSuccess)

This assumes that DeleteUserSuccess is changed to be defined with no type parameter:

type Msg =
  ...
  DeleteUserSuccess
like image 51
Chad Gilbert Avatar answered Oct 30 '22 21:10

Chad Gilbert