Last active
June 26, 2023 11:05
-
-
Save KoCMoHaBTa/871ac300f258c452902cf5657ea39f9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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