Meowthra
Meowthra

Reputation: 49

How to configure http request with "form" in elm

I just started learning elm this week so excuse me if the question is super basic.

So, I want to send a keyword (which is part of my Model, as shown in the forms example at https://elm-lang.org/examples/forms) as a "form" from my elm frontend to my backend. My cURL HTTP request looks like this:

curl -X 'POST' --form 'keyword=key' 0.0.0.0:5000/search

How would I transform that into an HTTP request written in elm, specifically the --form part? I read the HTTP section in the elm guide, but it doesn't mention anything about this.

Upvotes: 3

Views: 339

Answers (1)

glennsl
glennsl

Reputation: 29106

--form corresponds to a multipart request, which you can use Http.multipartBody to construct. This is the equivalent of your example curl request.

Http.post
    { url = "http://0.0.0.0:5000/search"
    , body =
        Http.multipartBody
            [ Http.stringPart "keyword" "key"
            ]
    , expect = Http.expectString GotText
    }

With a multipartBody you can also post files and blobs with filePart and bytesPart, respectively, in addition to simple key-value pairs as done here using stringPart

Upvotes: 5

Related Questions