gpt4 book ai didi

elm - 在榆树中进行 "Http.post"调用的方法是什么

转载 作者:行者123 更新时间:2023-12-04 05:16:00 25 4
gpt4 key购买 nike

我以下列方式在 elm 中使用了 http 帖子,但它无法正常工作,并且在服务器端我得到的是空的正文。
谁能告诉我这段代码有什么问题。这是我的命令模块的代码

module SignupForm.Commands exposing (..)

import Http exposing(..)
import Json.Decode as Decode exposing (field)
import SignupForm.Models exposing (..)
import SignupForm.Messages exposing (..)
import Json.Encode as Encode
import Debug exposing (log)




memberDecoder : Decode.Decoder UserDetails
memberDecoder =
Decode.map4 UserDetails
(field "fname" Decode.string)
(field "lname" Decode.string)
(field "email" Decode.string)
(field "password" Decode.string)



saveUrl : String
saveUrl =
"http://localhost:4000/userSignup"


saveRequest : UserDetails -> Http.Request UserDetails
saveRequest d =
Http.request


{ body = memberEncoded d |> Http.jsonBody
, expect = Http.expectJson memberDecoder
, headers = defaultRequestHeaders
, method = "POST"
, timeout = Nothing
, url = saveUrl
, withCredentials = False
}



defaultRequestHeaders : List Header
defaultRequestHeaders =
[ Http.header "Content-Type" "application/x-www-form-urlencoded"
]

fetchAll : UserDetails -> Cmd Msg
fetchAll data =
saveRequest data
|> Http.send OnSignUp


memberEncoded : UserDetails -> Encode.Value
memberEncoded data =
let
list =
[ ( "fname", Encode.string data.fname )
, ( "lname", Encode.string data.lname )
, ( "email", Encode.string data.email )
, ( "password", Encode.string data.password )
]
in
list
|> Encode.object

最佳答案

您正在发布 json,但声明您正在发送表单数据。

尝试删除:

defaultRequestHeaders : List Header
defaultRequestHeaders =
[ Http.header "Content-Type" "application/x-www-form-urlencoded"
]

(请注意,expectJson 会自动为您添加 Http.header "Content-Type" "application/json"

关于elm - 在榆树中进行 "Http.post"调用的方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41951022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com