API GET POST Method

//  ViewController.swift
//  JsonSerialiozationAPI


import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //getMethod()
        PostMethod()
     //   GetMethod1()
       }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

//func getMethod()
//    {
//        
//        let url = URL(string: "http://180.211.99.165/rs/ovg/api/v1/category_list")
//        
//        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
//            if error != nil
//            {
//                print("error")
//            }
//            else
//            {
//                if let content = data
//                {
//                    do
//                    {
//                        let myjson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
//                        print(myjson)
//                        
//                    }
//                    catch
//                    {
//                        
//                    }
//                }
//            }
//        }
//        task.resume()
//    }
//    
    
    
    
    
    func PostMethod()
    {
        let dict = ["name":"www","email":"qqq","suggestion":"eeee"]

        if  let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
          {
            let url = NSURL(string: "http://180.211.99.165/rs/ovg/api/v1/feedback")
            let request = NSMutableURLRequest(url: url as! URL)
            request.httpMethod = "POST"
           // request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = jsonData
            
            let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
                if error != nil{
                    print(error?.localizedDescription)
                    return
                }
                
                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                    print(json!)
                    
                } catch let error as NSError {
                    print(error)
                }        
            }          
            task.resume()
        }
    }

    
    
    
    
    
    
    
    func GetMethod1()
    {
            let url = NSURL(string:  "http://180.211.99.165/rs/ovg/api/v1/category_list")
        
            let request = NSMutableURLRequest(url: url as! URL)
        
            request.httpMethod = "GET"
        
        
            let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
                if error != nil{
                    
                    print(error?.localizedDescription)
                    return
                }
                
                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                    print(json!)
                    
                } catch let error as NSError {
                    print(error)
                }
            }
            task.resume()
        }
   

}

Comments