gpt4 book ai didi

ios - 如何使用 Alamofire 创建 post 方法 cURL 请求来实现 API?

转载 作者:行者123 更新时间:2023-11-29 05:37:59 26 4
gpt4 key购买 nike

我有 curl

curl --location --request POST "{{local_url}}/api/customer/registeration" \
--header "Content-Type: application/x-www-form-urlencoded" \
--header "device_id: jhjkhkjh" \
--header "device_type: android" \
--header "client_id: 1" \
--header "client_secret: DdoCiociSYHlrjkFrfwfAaNdJcFLjy676ff017zt" \
--data "name=ram&last_name=sham&mobile_number=7529883763&password=1&role_customer=home%20customer"

我想要在 Swift 中使用 Alamofire 实现注册 API,请帮助我。我尝试了以下代码,但收到错误代码 422。帮助我解决此问题。

    let urlString = "{{local_url}}/api/customer/registeration"
let parameters: [String: Any] = ["name":"Prashant", "last_name": "Kumar", "mobile_number":7814802725, "password": 1, "role_customer": "home%20customer"]
let headers = ["Content-Type": "application/x-www-form-urlencoded" ,"device_id": "jhjkhkjh", "device_type": "android", "client_id": "1", "client_secret": "DdoCiociSYHlrjkFrfwfAaNdJcFLjy676ff017zt"]
Alamofire.request(urlString, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
if let json = response.result.value {
print(json)
}
}

但我得到了结果。

{
data = {
};
error = {
code = 422;
"error_message" = {
message = (
"The name field is required.",
"The last name field is required.",
"The mobile number field is required.",
"The password field is required.",
"The role customer field is required."
);
};
};
status = 0;
}

最佳答案

您的编码错误。 JSONEncoding.default 用于当您当前正在发送表单数据而想要发送 JSON 数据时。尝试使用

URLEncoding.default

关于ios - 如何使用 Alamofire 创建 post 方法 cURL 请求来实现 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56845605/

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