투케이2K

338. (ios/swift5) URLSession 사용해 HTTP 통신 PUT 요청 수행 실시 본문

IOS

338. (ios/swift5) URLSession 사용해 HTTP 통신 PUT 요청 수행 실시

투케이2K 2023. 10. 8. 12:40

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT5

 

[소스 코드]

    // -----------------------------------------------------------------------------------------
    // MARK: - [PUT 방식 Body Json HTTP 통신 수행]
    // -----------------------------------------------------------------------------------------
    func requestPutBodyJsonHttp(tag: String, url: String, header: Dictionary<String, Any>?, params: Dictionary<String, Any>?, completion: @escaping (Bool, String)->()) {
        
        /*
         // -----------------------------------------
         [requestPutBodyJsonHttp 메소드 설명]
         // -----------------------------------------
         1. 비동기 PUT 방식 Body Json HTTP 통신 수행 및 콜백 반환 실시
         // -----------------------------------------
         2. 호출 방법 :
         
         let tag_ = "테스트 json 요청"
         let url_ = "http://jsonplaceholder.typicode.com/posts/1"
         let header_ : Dictionary<String, Any> = [:]
         let params_ : Dictionary<String, Any> = ["userId":1 , "id":1, "bar":"foo", "title":"foo"]
         
         
         N_AsyncHttp().requestPutBodyJsonHttp(tag: tag_, url: url_, header: header_, params: params_){(result, response) in
             
             S_Log._D_(description: "HTTP 콜백 결과 확인", data: [response])
             
         }
         // -----------------------------------------
         3. 사전 설정 사항 :
         
         - 필요 info plist 설정
         [1] http 허용 : App Transport Security Settings >> Allow Arbitrary Loads >> YES
         // -----------------------------------------
         */
        
        
        // -----------------------------------------
        // [url 선언 실시]
        // -----------------------------------------
        var urlData = String(describing: url)
        // -----------------------------------------
        
        
        // -----------------------------------------
        // [body json 생성]
        // -----------------------------------------
        var jsonData: Data? = nil
        if C_Util().dicNotNull(dic_: params) == true {
            
            jsonData = try! JSONSerialization.data(withJSONObject: params!, options: [])
            
        }
        
        // -----------------------------------------
        
        
        // -----------------------------------------
        // [URLRequest 생성 실시]
        // -----------------------------------------
        let urlComponents = URLComponents(string: urlData)
        var requestURL = URLRequest(url: (urlComponents?.url)!)
        
        requestURL.httpMethod = "PUT"
        
        requestURL.addValue("application/json", forHTTPHeaderField: "Content-Type") // header settings
        requestURL.addValue("no-cache", forHTTPHeaderField: "Cache-Control") // header settings
        
        if C_Util().dicNotNull(dic_: header) == true {
            
            for key in header!.keys {
                
                requestURL.addValue("\(String(describing: header![key] ?? ""))", forHTTPHeaderField: "\(key)")
                
            }

        }
        
        
        // [body 데이터 삽입]
        if jsonData != nil {
            requestURL.httpBody = jsonData // Body 부분에 Json 데이터 삽입 실시
        }
        
        
        S_Log._D_(description: "비동기 PUT 방식 Body Json HTTP [요청] 수행", data: [
            "TAG :: " + String(describing: tag),
            "TYPE :: PUT >> REQUEST",
            "URL :: " + String(describing: urlData),
            "HEADER :: " + String(describing: header),
            "PARAMS :: " + String(describing: params),
        ])
        
        
        // -----------------------------------------
        // [http 요쳥을 위한 URLSessionDataTask 생성]
        // -----------------------------------------
        let sessionConfig = URLSessionConfiguration.default
        sessionConfig.timeoutIntervalForRequest = N_AsyncHttp.connectTimeout // [커넥션 타임 아웃 설정]
        let session = URLSession(configuration: sessionConfig)
        
        let dataTask = session.dataTask(with: requestURL, completionHandler: { (data, response, error) in

            // [error가 존재하면 종료]
            guard error == nil else {
                S_Log._D_(description: "비동기 PUT 방식 Body Json HTTP [결과] 실패", data: [
                    "TAG :: " + String(describing: tag),
                    "TYPE :: PUT >> FAIL",
                    "URL :: " + String(describing: urlData),
                    "HEADER :: " + String(describing: header),
                    "PARAMS :: " + String(describing: params),
                    "ERROR :: \(error?.localizedDescription ?? "")"
                ])
                
                // [콜백 반환]
                completion(false, "[ERROR] :: \(error?.localizedDescription ?? "")")
                return
            }

            
            // [status 코드 체크 실시]
            let successsRange = 200..<300
            guard let statusCode = (response as? HTTPURLResponse)?.statusCode, successsRange.contains(statusCode)
            else {
                S_Log._D_(description: "비동기 PUT 방식 Body Json HTTP [결과] 에러", data: [
                    "TAG :: " + String(describing: tag),
                    "TYPE :: PUT >> ERROR",
                    "URL :: " + String(describing: urlData),
                    "HEADER :: " + String(describing: header),
                    "STATUS CODE :: " + String(describing: (response as? HTTPURLResponse)?.statusCode ?? 0),
                    "RESPONSE DATA :: \((response as? HTTPURLResponse)?.description ?? "")"
                ])
                
                // [콜백 반환]
                completion(false, "[ERROR] :: \((response as? HTTPURLResponse)?.description ?? "")")
                return
            }

            
            // [response 데이터 획득]
            let resultCode = (response as? HTTPURLResponse)?.statusCode ?? 0 // [상태 코드]
            let resultData = String(data: data!, encoding: .utf8) ?? "" // [데이터 확인]
            
            S_Log._D_(description: "비동기 PUT 방식 Body Json HTTP [결과] 확인", data: [
                "TAG :: " + String(describing: tag),
                "TYPE :: PUT >> RESPONSE",
                "URL :: " + String(describing: urlData),
                "STATUS CODE :: " + String(describing: resultCode),
                "RESPONSE DATA :: " + String(describing: resultData)
            ])
            
            // [콜백 반환]
            completion(true, resultData)
        })

        
        // [network 통신 실행]
        dataTask.resume()
    }
 

[결과 출력]

 

================================================================
LOG :: CLASS PLACE :: N_AsyncHttp.swift :: requestPutBodyJsonHttp(tag:url:header:params:completion:) :: 614
-------------------------------------------------
LOG :: NOW TIME :: 2023-10-08 12:27:22
-------------------------------------------------
LOG :: DESCRIPTION :: 비동기 PUT 방식 Body Json HTTP [요청] 수행
-------------------------------------------------
LOG :: TAG :: 테스트 json 요청
-------------------------------------------------
LOG :: TYPE :: PUT >> REQUEST
-------------------------------------------------
LOG :: URL :: http://jsonplaceholder.typicode.com/posts/1
-------------------------------------------------
LOG :: HEADER :: Optional([:])
-------------------------------------------------
LOG :: PARAMS :: Optional(["userId": 1, "bar": "foo", "id": 1, "title": "foo"])
================================================================



================================================================
LOG :: CLASS PLACE :: N_AsyncHttp.swift :: requestPutBodyJsonHttp(tag:url:header:params:completion:) :: 672
-------------------------------------------------
LOG :: NOW TIME :: 2023-10-08 12:27:24
-------------------------------------------------
LOG :: DESCRIPTION :: 비동기 PUT 방식 Body Json HTTP [결과] 확인
-------------------------------------------------
LOG :: TAG :: 테스트 json 요청
-------------------------------------------------
LOG :: TYPE :: PUT >> RESPONSE
-------------------------------------------------
LOG :: URL :: http://jsonplaceholder.typicode.com/posts/1
-------------------------------------------------
LOG :: STATUS CODE :: 200
-------------------------------------------------
LOG :: RESPONSE DATA :: {
  "userId": 1,
  "bar": "foo",
  "id": 1,
  "title": "foo"
}
================================================================

 

반응형
Comments