Skip to content

Instantly share code, notes, and snippets.

@KoCMoHaBTa
Last active June 26, 2023 11:05
Show Gist options
  • Save KoCMoHaBTa/871ac300f258c452902cf5657ea39f9a to your computer and use it in GitHub Desktop.
Save KoCMoHaBTa/871ac300f258c452902cf5657ea39f9a to your computer and use it in GitHub Desktop.
//
// URLRequest+HTTPMethod.swift
// https://gist.github.com/KoCMoHaBTa/871ac300f258c452902cf5657ea39f9a
//
// Created by Milen Halachev on 2/21/17.
// Copyright © 2017 Milen Halachev. All rights reserved.
//
import Foundation
extension URLRequest {
///HTTP method
///
///[Online Reference](https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html)
public enum HTTPMethod: String {
case get = "GET"
case put = "PUT"
case post = "POST"
case delete = "DELETE"
case head = "HEAD"
case options = "OPTIONS"
case trace = "TRACE"
case connect = "CONNECT"
}
public var method: HTTPMethod? {
get {
guard let httpMethod = self.httpMethod else { return nil }
let method = HTTPMethod(rawValue: httpMethod)
return method
}
set {
self.httpMethod = newValue?.rawValue
}
}
}
extension URLRequest {
public init(url: URL, method: HTTPMethod, contentType: String?) {
self.init(url: url)
self.method = method
self.setValue(contentType, forHTTPHeaderField: "Content-Type")
}
public init(url: URL, method: HTTPMethod, contentType: String?, body: Data?) {
self.init(url: url)
self.method = method
self.setValue(contentType, forHTTPHeaderField: "Content-Type")
self.httpBody = body
}
public init(url: URL, method: HTTPMethod, contentType: String?, cachePolicy: CachePolicy, timeoutInterval: TimeInterval) {
self.init(url: url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
self.method = method
self.setValue(contentType, forHTTPHeaderField: "Content-Type")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment