Ios Post方法未在api调用中发送我的数据
嗨,我是swift的新手,我做了以下api调用来发送数据,但它没有发送,我得到了以下响应 发送数据 响应 但我可以获得所有其他验证消息,如“用户名不能为空” 但我尝试了Postman,它也给出了与上面标题方法相同的错误消息,但后来我发现并发送了Body方法和form of application/x-www-form-urlencoded,然后我得到了如下成功响应 下面是我的API调用。。。请有人找出我做错了什么,或者给我建议一个更好的后api调用 还有一件事,这是我为“/homefeed”创建的相同API调用方法,并得到了响应,但我们不需要为此发送任何特定参数。请帮帮我Ios Post方法未在api调用中发送我的数据,ios,swift,post,httprequest,swifty-json,Ios,Swift,Post,Httprequest,Swifty Json,嗨,我是swift的新手,我做了以下api调用来发送数据,但它没有发送,我得到了以下响应 发送数据 响应 但我可以获得所有其他验证消息,如“用户名不能为空” 但我尝试了Postman,它也给出了与上面标题方法相同的错误消息,但后来我发现并发送了Body方法和form of application/x-www-form-urlencoded,然后我得到了如下成功响应 下面是我的API调用。。。请有人找出我做错了什么,或者给我建议一个更好的后api调用 还有一件事,这是我为“/homefeed”创
func addNewUser()
{
let url = URL(string: "https://xxxxxxxxxx.azurewebsites.net/api/addUser")!
let firstName:String = firstnameTextFeild.text!
let lastName:String = lastNameTxtField.text!
let username:String = usernameTextField.text!
let password:String = passwordTextField.text!
let email:String = emailTextField.text!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "firstName=\(firstName)&lastName=\(lastName)&username=\(username)&password=\(password)&email=\(email)&latitude=\(lat)&longitude=\(long)"
print("Sent Data -",postString)
request.httpBody = postString.data(using: .utf8)
/*
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
} catch let error {
print(error.localizedDescription)
}
*/
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
// print("responseString = \(String(describing: responseString))")
print("responseString ", responseString ?? "resSt")
}
task.resume()
}
您应该像这样传递参数:
let passingDict : [String:Any] = [
"fname" : YourValue,
"lname":YourValue,
"email" : YourValue,
"password" : YourValue,
"countryCode": YourValue,
"mobileNumber": YourValue,
"verificationType":YourValue,
]
let signup_url = ApiList.base_url + ApiList.signup_url
singletonclass.instance.Post_API_Call(passingDict, Url: signup_url, HittingApi: "SIGN_UP_URL")
func Post_API_Call(_ Parame: NSDictionary, Url: String, HittingApi: String)
{
var Api_Resp_Err = String()
var Api_Resp_Dict = NSDictionary()
do
{
let jsonData = try JSONSerialization.data(withJSONObject: Parame, options: [])
let fullListURL = Url
let url = URL(string: Url)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data,response,error in
if error != nil {
DispatchQueue.main.async(execute: {
Api_Resp_Err = (error?.localizedDescription)!
})
return
}
do
{
if let responseJSON = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
{
if responseJSON.count > 0
{
DispatchQueue.main.async(execute: {
let statusCode = responseJSON.object(forKey: "code") as? String ?? ""
let message = responseDict.object(forKey: "Message") as? String ?? ""
})
}
else
{
}
}
else
{
DispatchQueue.main.async(execute: {
Api_Resp_Err = "Data Issue"
})
}
}
catch
{
DispatchQueue.main.async(execute: {
Api_Resp_Err = "Catch Issue"
})
}
}
task.resume()
}
catch
{
DispatchQueue.main.async(execute: {
//print("Server Issue catch II")
})
}
}
您应该像这样传递参数:
let passingDict : [String:Any] = [
"fname" : YourValue,
"lname":YourValue,
"email" : YourValue,
"password" : YourValue,
"countryCode": YourValue,
"mobileNumber": YourValue,
"verificationType":YourValue,
]
let signup_url = ApiList.base_url + ApiList.signup_url
singletonclass.instance.Post_API_Call(passingDict, Url: signup_url, HittingApi: "SIGN_UP_URL")
func Post_API_Call(_ Parame: NSDictionary, Url: String, HittingApi: String)
{
var Api_Resp_Err = String()
var Api_Resp_Dict = NSDictionary()
do
{
let jsonData = try JSONSerialization.data(withJSONObject: Parame, options: [])
let fullListURL = Url
let url = URL(string: Url)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data,response,error in
if error != nil {
DispatchQueue.main.async(execute: {
Api_Resp_Err = (error?.localizedDescription)!
})
return
}
do
{
if let responseJSON = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
{
if responseJSON.count > 0
{
DispatchQueue.main.async(execute: {
let statusCode = responseJSON.object(forKey: "code") as? String ?? ""
let message = responseDict.object(forKey: "Message") as? String ?? ""
})
}
else
{
}
}
else
{
DispatchQueue.main.async(execute: {
Api_Resp_Err = "Data Issue"
})
}
}
catch
{
DispatchQueue.main.async(execute: {
Api_Resp_Err = "Catch Issue"
})
}
}
task.resume()
}
catch
{
DispatchQueue.main.async(execute: {
//print("Server Issue catch II")
})
}
}
使用此代码。它对您有效 安装播客文件 阿拉莫菲尔吊舱 在ViewController中导入Alamofire
func addNewUser(){
let url = "your URL"
var param : [String : AnyObject] = [:]
param = ["firstName": firstnameTextFeild.text! as AnyObject,
"lastName": lastNameTxtField.text! as AnyObject,
"username": usernameTextField.text! as AnyObject,
"password": passwordTextField.text! as AnyObject,
"email": emailTextField.text! as AnyObject,
"latitude": "your latitude" as AnyObject,
"longitude": "your longitude" as AnyObject]
print(param)
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding()).responseJSON { (response:DataResponse<Any>) in
print(response)
if (response.result.value != nil) {
//your code
}
else{
//your code
}
}
}
func addNewUser(){
让url=“您的url”
变量参数:[字符串:AnyObject]=[:]
param=[“firstName”:firstnameTextFeild.text!作为任何对象,
“lastName”:lastNameTxtField.text!作为任何对象,
“username”:usernameTextField.text!作为任何对象,
“password”:passwordTextField.text!作为任何对象,
“email”:emailTextField.text!作为任何对象,
“纬度”:“你的纬度”作为任何物体,
“经度”:“您的经度”作为任何对象]
打印(参数)
请求(url,方法:.post,参数:param,编码:URLEncoding()).responseJSON{(响应:DataResponse)在
打印(答复)
if(response.result.value!=nil){
//你的代码
}
否则{
//你的代码
}
}
}
使用此代码。它对您有效
安装播客文件
阿拉莫菲尔吊舱
在ViewController中导入Alamofire
func addNewUser(){
let url = "your URL"
var param : [String : AnyObject] = [:]
param = ["firstName": firstnameTextFeild.text! as AnyObject,
"lastName": lastNameTxtField.text! as AnyObject,
"username": usernameTextField.text! as AnyObject,
"password": passwordTextField.text! as AnyObject,
"email": emailTextField.text! as AnyObject,
"latitude": "your latitude" as AnyObject,
"longitude": "your longitude" as AnyObject]
print(param)
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding()).responseJSON { (response:DataResponse<Any>) in
print(response)
if (response.result.value != nil) {
//your code
}
else{
//your code
}
}
}
func addNewUser(){
让url=“您的url”
变量参数:[字符串:AnyObject]=[:]
param=[“firstName”:firstnameTextFeild.text!作为任何对象,
“lastName”:lastNameTxtField.text!作为任何对象,
“username”:usernameTextField.text!作为任何对象,
“password”:passwordTextField.text!作为任何对象,
“email”:emailTextField.text!作为任何对象,
“纬度”:“你的纬度”作为任何物体,
“经度”:“您的经度”作为任何对象]
打印(参数)
请求(url,方法:.post,参数:param,编码:URLEncoding()).responseJSON{(响应:DataResponse)在
打印(答复)
if(response.result.value!=nil){
//你的代码
}
否则{
//你的代码
}
}
}
这将对您有所帮助:@Rocky我正在使用来自该帖子的相同api调用。。我可以得到api的那些没有参数发送的成功响应。但是当我发送参数时,我得到了“出错消息”,取消注释JSONSerialization
语句,添加application/x-www-form-urlencoded;charset=utf-8
到内容类型
,并在您的问题中添加参数
变量。这将帮助您:@Rocky我正在使用来自该帖子的相同api调用。。我可以得到api的那些没有参数发送的成功响应。但是当我发送参数时,我得到了“出错消息”,取消注释JSONSerialization
语句,添加application/x-www-form-urlencoded;字符集=utf-8
至内容类型
,还可以在你的问题中添加参数
变量。不,我仍然得到sameI尝试了许多不同的发帖方法,但我得到了相同的消息是的,我仔细检查了参数,它是正确的。当参数不正确时,你可以检查问题中的邮递员屏幕截图,只有这类问题出现。我有一些可选参数对于此api,我将尝试使用所有参数并让您知道。。谢谢你的帮助不,我仍然得到sameI尝试了许多不同的post方法,但我得到了相同的消息是的,我仔细检查了参数它是正确的,你可以检查问题中的邮递员屏幕截图当参数不正确时,只有这种类型的问题出现。我有一些可选参数用于此api,我将尝试所有参数并让您知道。。谢谢你的大力帮助