Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Vladimir Zhelnov - neatek.pw - Web/iOS dev
- class Class_JSONRequest {
- func get_url_post(get_url: String, params: String, completion: @escaping (_ result: NSDictionary) -> Void) {
- let myUrl = URL(string: get_url);
- var request = URLRequest(url:myUrl!)
- request.httpMethod = "POST"
- let postString = params;
- request.httpBody = postString.data(using: String.Encoding.utf8);
- let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
- if error != nil
- {
- print("error=\(error)")
- return
- }
- do {
- if(data != nil) {
- let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
- if let parseJSON = json {
- //print(parseJSON)
- completion(parseJSON)
- }
- }
- } catch {
- print(error)
- }
- }
- task.resume()
- }
- }
- /* usage:
- let post_params = "some=post¶ms=1";
- JSON.get_url_post(get_url: "http://..api", params: post_params , completion: { answer in
- ... something
- });
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement