gpt4 book ai didi

iOS Swift Http 请求 post 方法使用 header x-www-form-urlencoded?

转载 作者:行者123 更新时间:2023-11-30 11:20:57 26 4
gpt4 key购买 nike

我必须使用 application/x-www-form-urlencoded 值作为 JSON 字符串进行 API 调用。当在 postman 中给出参数值和 header 时,它工作正常并返回状态代码 200 ok。这里我使用后端 node js 。 post方法在前端不起作用。不知道是什么问题。

错误:

Sometimes i am getting request time out, 
NSUrlfailingstring, finished with status code 1001

这是我的后端代码:

  var status = {
SUCCESS : 'success',
FAILURE : 'failure'
}

var httpStatus = {
OK : HttpStatus.OK,
ISE : HttpStatus.INTERNAL_SERVER_ERROR,
BR : HttpStatus.BAD_REQUEST
}
exports.likes= function(req, res){

var Username =req.body.username;
var likecount=req.body.count;
var likedby = req.body.likedby;
var postId = req.body.postId;
var tokenId = req.body.tokenId;
var message = {
to: tokenId,
collapse_key: '',
data: {
name:Username,
Message:"Content about message",
Redirect:"TopostId : "+postId,
time: ""
},
notification: {
title: "Hey Buddy , Someone have liked your post",
body: likedby +"Likedyourpost",
icon: "notification"
}
};


fcm.send(message)
.then(function (response) {
console.log("Successfully sent with response: ", response);
res.status(httpStatus.OK).json({
status: status.SUCCESS,
code: httpStatus.OK,
error:''
});
return;
})
.catch(function (err) {
console.log(err);

});

};


module.exports = function(app) {
app.post('/likesnotification', notification.likes);
app.post('/commentsnotification', notification.comments);
app.post('/othernotification', notification.othernot);
app.post('/followrequset', notification.followreq);
app.post('/followresponse', notification.followres);
app.post('/publicaccountfollow', notification.publicacfollow);

};

这是我在 ios Swift 中的前端代码:

尝试 1:

   func postNotification(postItem: String, post: Post) {

print("Get token from post:::",post.token)
print(postItem)
let token = UserDefaults.standard.string(forKey: "token")




let headers: HTTPHeaders = ["Content-Type" :"application/x-www-form-urlencoded"]

let parameters : [String:Any] = ["count":post.likeCount!, "likedby":currentName, "postId=":postItem, "token": post.token!]
Alamofire.request("http://highavenue.co:9000/likesnotification/", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

switch(response.result) {
case .success(_):
if let data = response.result.value{
print(data)
}
break

case .failure(_):
print(response.result.error as Any)
break

}
}

}

尝试 2:

 var parameters       = [String:Any]()

parameters["count"] = post.likeCount!
parameters["likedby"] = currentName
parameters["postId"] = postItem
parameters["token"] = post.token!

let Url = String(format: "http://highavenue.co:9000/likesnotification")
guard let serviceUrl = URL(string: Url) else { return }
// let loginParams = String(format: LOGIN_PARAMETERS1, "test", "Hi World")
let parameterDictionary = parameters
var request = URLRequest(url: serviceUrl)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
return
}
request.httpBody = httpBody

let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options:
[])
print(json)
}catch {
print(error)
}
}
}.resume()

非常感谢任何帮助。

最佳答案

我看到你的代码,你应该调用你为其创建的 header 参数。您没有在 alamofire 请求方法中传递 header 。

如下:

let headers: HTTPHeaders = ["Content-Type" :"application/x-www-form-urlencoded"]

let parameters : [String:Any] = ["count":post.likeCount!, "likedby":currentName, "postId=":postItem, "token": post.token!]
Alamofire.request("http://highavenue.co:9000/likesnotification/", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response:DataResponse<Any>) in

switch(response.result) {
case .success(_):
if let data = response.result.value{
print(data)
}
break

case .failure(_):
print(response.result.error as Any)
break

}
}

}

关于iOS Swift Http 请求 post 方法使用 header x-www-form-urlencoded?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51277355/

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