Elm json decoding type error

I am new to Elm and I am trying to decode a json response but returning back to view is having errors please i need ideas seems i am missing something

type alias Entry =
    { title : String
    , department_name : String
    , external_reference_id : String
    }

decoderEntry : Decode.Decoder Entry
decoderEntry =
    Json.Decode.Pipeline.decode Entry
        |> required "title" Decode.string
        |> required "department_name" Decode.string
        |> required "external_reference_id" Decode.string



decodeEntry : Decode.Decoder Entry -> String -> Result String Entry
decodeEntry decoder =
    Decode.decodeString decoder

then my request object looks like this

getEntry : String -> Http.Request (Entry)
getEntry reqval =
    Http.get ("api/entry/job/" ++ reqval)
        (decodeEntry decoderEntry)

For now i get the error

Function `get` is expecting the 2nd argument to be:

    Json.Decode.Decoder a

But it is:

    String -> Result String Requisition

Not sure what decodeRequisition does, or if you have your own Http package…
But the normal Http.get works something like this:
(You never use decodeString by yourself)

getEntry: String -> Cmd Msg
getEntry reqval =
  Http.get
    { url = "api/entry/job/" ++ reqval
    , expect = Http.expectJson GotEntry decoderEntry 
    }

type alias Entry =
    { title : String
    , department_name : String
    , external_reference_id : String
    }

decoderEntry : Decode.Decoder Entry
decoderEntry =
    Json.Decode.Pipeline.decode Entry
        |> required "title" Decode.string
        |> required "department_name" Decode.string
        |> required "external_reference_id" Decode.string

Where:

type Msg 
  = .......
  | GotEntry (Result Http.Error Entry)
  | ....
  | ....
  ..

You can find some nice examples in the HTTP docs:
https://package.elm-lang.org/packages/elm/http/latest/Http

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.