Here i am sending parameter value like username, user toke, post id, etc to backend using alamofire. if status success then, notification will will send from backend. Inside postnotification function i have tried post method code using alamofire and datatask method but it does not work. In console i am getting request time out or nothing.
Here is my code :
func postNotification(postItem: String, post: Post) { // declare parameter as a dictionary which contains string as key and value combination. considering inputs are valid print("Get token from post:::",post.token) print(postItem) let token = UserDefaults.standard.string(forKey: "token") //create the url with URL 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 = ["username" : "Test", "password" : "123456"] 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() // let headers: HTTPHeaders = ["Content-Type" :"application/x-www-form-urlencoded"] // // Alamofire.request("http://highavenue.co:9000/likesnotification", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in // // original URL request // print("Request is :",response.request!) // // // HTTP URL response --> header and status code // print("Response received is :",response.response) // // // server data : example 267 bytes // print("Response data is :",response.data) // // // result of response serialization : SUCCESS / FAILURE // print("Response result is :",response.result) // // debugPrint("Debug Print :", response) // // // } // Alamofire.request("http://highavenue.co:9000/likesnotification", method: HTTPMethod.post, parameters: json, encoding: JSONEncoding.default, headers: headers).responseJSON { response in // // // original URL request // print("Request is :",response.request!) // // // HTTP URL response --> header and status code // print("Response received is :",response.response) // // // server data : example 267 bytes // print("Response data is :",response.data) // // // result of response serialization : SUCCESS / FAILURE // print("Response result is :",response.result) // // debugPrint("Debug Print :", response) // } }
Any help much appreciated pls..
1 Answers
Answers 1
Yaah solved it. There was a negligence mistake. I used an additional slash in the URL. I changed the web API to a different folder and I made this mistake while changing it in the iOS code. and Also Set your timeout interval here.
let RequestData = NSMutableURLRequest(URL: NSURL.init(string: "Your URL Hear")!) RequestData.HTTPMethod = "POST" RequestData.timeoutInterval = 250 // Time interval here. Alamofire.request(RequestData).responseJSON { (responseData) -> Void in if((responseData.result.value) != nil) { // response print(responseData.result.value!) } }
Hope this Help You..
0 comments:
Post a Comment