URLRequest 뭘까 ?

조영민·2022년 7월 4일
0

URLRequest includes the HTTP method (GET, POST, and so on) and the HTTP headers.

  1. Get ->
    url + HttpMethod만 정해주면된다.
    심지어 httpMethod기본값 get

  2. Post ->
    HTTPHeader들을 넣어주어야 한다.
    setValue로

    "Content-Type": "application/json",

이런게 있는 배열을 request에 해주면 된다.

  1. 둘다
    HTTPBody -> Json이면 Json형식으로 바디를 짜주자.

  2. URL 구성 요소

  let urlQueries = self.params.queryParam.map { URLQueryItem(name: $0.key, value: $0.value) }

path들로 query들을 요청에 맞게 바꾸고 url에 추가하고

 urlComponents?.queryItems = urlQueries
       
       if let url = urlComponents?.url {
           var request = URLRequest(url: url)
           request.httpMethod = self.method.rawValue
           request.allHTTPHeaderFields = self.httpHeader
           
           return request
       }

이렇게 해주면 된다 !

API요청 구조가 잘 이해가 안되었지만, 정리가 된 기분이다 !

0개의 댓글