macOS, iOS

[iOS] API의 Body type 설정하기 - none, form-data, x-www-form-urlencoded, raw, binary,...

SweetDev 2021. 4. 5. 18:57

none, form-data, x-www-form-urlencoded, raw, binary...

다 다른 타입이다!!

 

나는 그중에도 raw타입- JSON 으로 보내줘야 했다. 

 

그러려면 다음과 같이 헤더 설정을 Content-Type: application/json으로 해줘야 한다!!!!!!

 

enum BodyType: String {
  case none
  case form_data = "form-data"
  case lengthZero
  case json = "json"

  var header: [String]? {
    switch self {
    case .form_data:
      return ["Content-Type", "multipart/form-data"]
    case .lengthZero:
      return ["Content-Length", "0"]
    case .json:
      return ["Content-Type", "application/json"]
    default:
      return nil
    }
  }
}

 

이런식으로 enum을 새로 만들었다. 

그리고 bodyType을 URLRequest를 만들어주는 함수에 넣었다.