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

What is the best way to handle an empty (no line at all) response?

Although the response code is 200, Elm returns an error because the empty response is not 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 is a POST action, so I cannot use getString .

+5
source share
2 answers

You can use the getString function from the Http module. This will return you any string returned from the HTTP request without trying to convert to a Json value.

If you need to use Http.send , you can do something like this:

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

This assumes that DeleteUserSuccess modified to define without a type parameter:

 type Msg = ... DeleteUserSuccess 
+3
source

It looks like you will never get a Json response, so you probably would be better off using Http.getString

 type Result = FetchFail Error | DeleteUserSuccess Http.getString address |> Task.perform FetchFail (\s -> DeleteUserSuccess) 

Since a successful get does not contain any information, you can simply ignore it and return DeleteUserSuccess regardless of the contents of the string.

0
source

All Articles