1 year ago
#297964
Panks
Timeout error when post public key string with URLSession
I am creating a rsa key pair and send the public key to my api. But the post request fails with Timeout error. Following is my function for the request:
func perform<T: Decodable>(reqData: Data, with completion: @escaping (T?) -> Void) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
// insert json data to the request
request.httpBody = reqData
print("jsonData: ", String(data: request.httpBody!, encoding: .utf8) ?? "no body data")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let error = error else {
print(error)
completion(nil)
return
}
guard let data = data else {
completion(nil)
return
}
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
completion(try? decoder.decode(T.self, from: data))
}
task.resume()
}
JSON Object sent in POST:
{
"publicKey" : "MIIBCgKCAQEA4lS13W+TXZhUXy5yB2NpeulCVb1ZSaReopeKrahjKmUx4NQxVXruEYCY3LpjZcSy8xiudVG3GBIMnPLtaMbc5WAYDj1M2OwnpNHdQ8SKtZ1tdA6iRjfOXGUa1n8FMIMKU5ynTSAiSoh+8gGrY0L6jTsCSdLO5ZU53LQFHSESM8JuBeNZozolb\/cKb38ylercVeVpo8egoA8UqHezK23VUJ23faxMmMZDJxVn5pfFedxBTLxwU65KQY8Z4izaFjuPLoGe5JZkXyYNMcrYloDCBG5m9BFiXVkoLEDmGFGfWLMJcqL+D2CszqJ4h712ZR6LYRNtIbo\/HG9KR7XCtap3QwIDAQAB"
}
I have tested the url in Postman and it works good there. But it is giving my follwoing error when I call it from my app:
Task <6AB1B692-9CBF-4E37-80C6-4F37FD43264B>.<1> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x280299c50 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <6AB1B692-9CBF-4E37-80C6-4F37FD43264B>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <6AB1B692-9CBF-4E37-80C6-4F37FD43264B>.<1>"
), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=http://192.168.178.205:3000/savepublickey, NSErrorFailingURLKey=http://192.168.178.205:3000/savepublickey, _kCFStreamErrorDomainKey=4}
Following is my server side code in Node.js:
app.post('/savepublickey', (req, res) => {
console.log("savepublickey");
console.log(req.body);
let data = JSON.parse(JSON.stringify(req.body, null, 2));
if (data['publicKey'] != null && data['publicKey'] != "") {
console.log("Success");
publicKey = data['publicKey'];
res.status(200).json({
"Status": "Success"
}).send();
} else {
console.log("Fail");
res.status(500).json({
"Status": "Invalid request"
}).send();
}
})
ios
swift
nsurlsession
urlsession
0 Answers
Your Answer