gpt4 book ai didi

ios - Alamofire POST 请求,正文中有一个简单的字符串作为参数, header 仍然显示缺少的参数值

转载 作者:行者123 更新时间:2023-11-28 07:32:32 25 4
gpt4 key购买 nike

即使传递了参数和头文件,也能从服务器得到响应

我还尝试了 encoding: JSONEncoding.defaultJSONEncoding.JSON

let URL_GET_DATA = http://abcdef.com:4207/base/signin

let headers = [
"Authorization": "Bearer 5baa9fd27862adc397033f1ff27d73b0c2318144",
"Content-Type": "application/x-www-form-urlencoded"
]

let parameters: Parameters = [
"Start": "1",
"end" : "11"
]
Alamofire.request(URL_GET_DATA, method: .post, parameters: parameters as! Parameters, headers: headers).responseJSON { response in
print("Request \(response.request)")

print("RESPONSE \(response.result.value)")
print("RESPONSE \(response.result)")
print("RESPONSE \(response)")


switch response.result {
case .success:
print(response)
let dictVal = response.result.value
let dictMain:NSDictionary = dictVal as! NSDictionary
let statusCode = dictMain.value(forKey: "statusCode") as! Int
let statusMsg = dictMain.value(forKey: "statusMsg") as! String
print(statusMsg)
print(statusCode)
//if the auth is sucessfull then
if(statusCode == 0)
{
let dictVal1 = dictMain.value(forKey: "data") as! NSDictionary
//get the responce code or Token From the server
let statusCode1 = dictVal1.value(forKey: "accessToken") as! String
let statusMsg1 = dictVal1.value(forKey: "refreshTokenExpiresAt") as! String

//Store data Locally to create session
UserDefaults.standard.set(false, forKey: "userSession")
print(statusCode1)
print(statusMsg1)

//dissmiss the screen and go to the home page
//self.dismiss(animated: true, completion: nil)


}
// if the auth is Failre
else if(statusCode == 1)
{
self.displayMyAlertMessage(userMessage: "Please Enter The Correct Credencial, Username or pasword is incorrect ")
}
case .failure(let error):
print(error)
self.displayMyAlertMessage(userMessage: "You are not Connected to the Internet Please Check The connection. \(error)")
}
}

}

最佳答案

  let URL_GET_DATA = "http://abcdef.com:4207/base/signin"


let parameters = [
"email_id": userEmail, //email
"password": userPassword //password
]
var statusCode: Int = 0
print(parameters)
//Almofire Code
Alamofire.request(URL_GET_DATA, method: .post, parameters: parameters).responseJSON { response in
print(response)

switch response.result {
case .success:
print(response)
let dictVal = response.result.value
let dictMain:NSDictionary = dictVal as! NSDictionary
let statusCode = dictMain.value(forKey: "statusCode") as! Int
let statusMsg = dictMain.value(forKey: "statusMsg") as! String
print(statusMsg)
print(statusCode)

//if the auth is sucessfull then
if(statusCode == 0)
{
let dictVal1 = dictMain.value(forKey: "data") as! NSDictionary
//get the responce code or Token From the server
var statusCode1="Bearer "
statusCode1 += dictVal1.value(forKey: "accessToken") as! String
// let statusCode1 = dictVal1.value(forKey: "accessToken") as! String
let statusMsg1 = dictVal1.value(forKey: "refreshTokenExpiresAt") as! String

//Store data Locally to create session
UserDefaults.standard.set(false, forKey: "userSession")
UserDefaults.standard.set(statusCode1, forKey: "accessToken")

//to pass the email to the Header file
UserDefaults.standard.set(userEmail, forKey: "emailSession")
let emailId = UserDefaults.standard.object(forKey: "emailSession")
print(emailId!)
// print(statusCode1)
// print(statusMsg1)

//dissmiss the screen and go to the home page
self.dismiss(animated: true, completion: nil)


}

// if the auth is Failre
else if(statusCode == 1)
{
self.displayMyAlertMessage(userMessage: "Please Enter The Correct Credencial, Username or pasword is incorrect ")

}
case .failure(let error):
print(error)
self.displayMyAlertMessage(userMessage: "You are not Connected to the Internet Please Check The connection. \(error)")
}
}

**这是有效的代码**

关于ios - Alamofire POST 请求,正文中有一个简单的字符串作为参数, header 仍然显示缺少的参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54274167/

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