Hi, I am incredibly new to elm and I am currently running into this problem. I have this web app that I am running on my local machine. It is supposed to send a POST method to my backend, which is also local. Here is my elm code:
module HomePage exposing (main)
import Browser
import Html exposing (…)
import Html.Attributes exposing (…)
import Html.Events exposing (…)
import Http
import Json.Encode
import Json.Decode exposing (list, string)
– VIEW
view : Model -> Html Msg
view model =
div [ class “jumbotron” ]
[ h1 [] [ text “Welcome to Gutenberg Project Book Recommender” ]
, h2 [] [text "Find books similar to: "]
, viewInput “text” “Keyword” model.keyword Keyword
, button [ onClick SubmitForm ] [ text “Go!” ]
, viewSearch model]
viewInput : String -> String -> String -> (String -> msg) -> Html msg
viewInput t p v toMsg =
input [ type_ t, placeholder p, value v, onInput toMsg ] []
viewSearch: Model -> Html Msg
viewSearch model =
case model.searchResponse of
SearchLoading ->
div []
[ text “Waiting…” ]
SearchFailure ->
div []
[ text “Search failed, please try again.” ]
SearchSuccess list ->
div []
[ text “Are you looking for…” ]
– this branch is currently for test purposes and not finished
– MODEL
type alias Model =
{ keyword : String
, searchResponse : SearchStatus}
type SearchStatus
= SearchLoading
| SearchFailure
| SearchSuccess (List String)
initialModel : Model
initialModel =
{ keyword = “”
, searchResponse = SearchLoading
}
init : () -> ( Model, Cmd Msg)
init _ =
(initialModel, Cmd.none)’
– UPDATE
type Msg
= Keyword String
| NoOp
| SubmitForm
| GotSearch (Result Http.Error (List String))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Keyword keyword ->
( { model | keyword = keyword }, Cmd.none )
NoOp ->
( model, Cmd.none )
SubmitForm ->
( { model | searchResponse = SearchLoading }, search model)
GotSearch result ->
case result of
Ok list ->
({ model | searchResponse = SearchSuccess list }, Cmd.none)
Err _ ->
({ model | searchResponse = SearchFailure }, Cmd.none)
– HTTP
search : Model -> Cmd Msg
search model =
Http.post
{ url = “http://0.0.0.0:5000/search”
, body = Http.stringBody model.keyword model.keyword
, expect = Http.expectJson GotSearch (Json.Decode.list string)
}
– SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view}
When I test this code on my browser, the message “Search failed, please try again.” appears, which seems to indicate that expectJSON returned an error. On the terminal where my backend is running, I see this message:
127.0.0.1 - - [22/Aug/2020 06:12:32] “POST /search HTTP/1.1” 400 -
I am having trouble debugging and obtaining HTTP metadata. Any help would be appreciated!