I'm trying to use elm-lang/http without success so far. I want to address 
an API with a POST request via JSON. All my efforts so far result in having 
the wrong headers.

There are two things I've tried in the following: in both cases the server 
tells me that it wasn't a POST request but OPTIONS.

I've tried using "Http.post":

jsonify : String -> Http.Body
jsonify str =
    Http.jsonBody <| Encode.object [("sentiment", Encode.string str)]


decodeJson : Decode.Decoder String
decodeJson =
    Decode.map2 (\classification status -> classification)
        (Decode.field "classification" Decode.string)
        (Decode.field "status" Decode.int)


fetchSentiment : String -> Cmd Msg
fetchSentiment sentiment =
    Http.send Fetch (Http.post (jsonify sentiment) decodeJson)

and a custom request:

fetchSentiment : String -> Cmd Msg
fetchSentiment sentiment =
    Http.send Fetch (postSentiment sentiment decodeJson)

postSentiment : String -> Decode.Decoder String -> Http.Request String
postSentiment sentiment decoder =
    Http.request
        { method = "POST"
        , headers = [(Http.header "Content-Type" "application/json")]
        , url = "http://127.0.0.1:5000/sentiment";
        , body = (jsonify sentiment)
        , expect = Http.expectJson decoder
        , timeout = Nothing
        , withCredentials = False
        }

Is my problem related to the code above, and if so, how can I fix it?

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to