在 Elm 中,GET 请求不能有正文,是吗?

ube*_*ben 3 http-get http-post request payload elm

Http.requestbody该方法似乎忽略了GET

init : () -> ( Model, Cmd Msg )
init _ =
    ( Loading
    , Http.request
        { method = "GET"
        , headers = []
        , url = "http://127.0.0.1"
        , body = Http.stringBody "text/plain" "Hello World!"
        , expect = Http.expectWhatever Sent
        , timeout = Nothing
        , tracker = Nothing
        }
    )
Run Code Online (Sandbox Code Playgroud)

发送的请求没有正文(使用浏览器开发工具检查时)。

init : () -> ( Model, Cmd Msg )
init _ =
    ( Loading
    , Http.request
        { method = "POST" {- CHANGED TO POST -}
        , headers = []
        , url = "http://127.0.0.1"
        , body = Http.stringBody "text/plain" "Hello World!"
        , expect = Http.expectWhatever Sent
        , timeout = Nothing
        , tracker = Nothing
        }
    )
Run Code Online (Sandbox Code Playgroud)

但是方法一改"POST",就有效了!体内含有"Hello World!".

我尝试与之通信的 API 需要请求application/json中包含正文GET。帮我 !

PS:这是文档的内容:

emptyBody : Body

为您的请求创建一个空正文。这对于不发送任何数据的 GET 请求和 POST 请求很有用。

这还不清楚,因为它可以用两种不同的方式解释:

这对于 GET 请求和{不发送任何数据的 POST 请求}很有用。

或者:

这对于不发送任何数据的 { GET 请求和 POST 请求 } 很有用。

gle*_*nsl 6

根据HTTP规范,GET不应该有主体。参见示例MDN 上的描述,其中写道:

\n
\n

注意:在请求中发送正文/有效负载GET可能会导致某些现有\n实现拒绝请求\xe2\x80\x94,而规范并未禁止\n,语义未定义。最好避免在GET请求中发送有效负载。

\n
\n