@@ -67,7 +67,7 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
67
67
68
68
// MARK: - IBAction
69
69
70
- /// GET request URL
70
+ /// GET / POST request URL
71
71
72
72
@IBAction func requestURLBtnClicked( _ sender: NSButton ) {
73
73
@@ -77,7 +77,27 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
77
77
print ( " encode URL = \( urlTF. stringValue) " )
78
78
UserDefaults . standard. setValue ( urlString, forKey: LastInputURLCacheKey)
79
79
let session = URLSession . shared
80
- let task = session. dataTask ( with: URL ( string: urlString) !) { [ weak self] ( data, response, error) in
80
+
81
+ let url = URL ( string: urlString)
82
+ var request = URLRequest ( url: url!, cachePolicy: . useProtocolCachePolicy, timeoutInterval: 30 )
83
+
84
+ if reqTypeBtn. indexOfSelectedItem == 1 {
85
+ if let query = url? . query {
86
+ urlString = urlString. replacingOccurrences ( of: query, with: " " )
87
+ if urlString. hasSuffix ( " ? " ) {
88
+ urlString. removeLast ( )
89
+ }
90
+ request = URLRequest ( url: URL ( string: urlString) !, cachePolicy: . useProtocolCachePolicy, timeoutInterval: 30 )
91
+ if let httpBody = query. data ( using: . utf8) {
92
+ print ( " httpBody query = \( query) " )
93
+ request. httpBody = httpBody
94
+ }
95
+ }
96
+ request. setValue ( " application/x-www-form-urlencoded " , forHTTPHeaderField: " Content-Type " )
97
+ request. httpMethod = " POST "
98
+ }
99
+
100
+ let task = session. dataTask ( with: request) { [ weak self] ( data, response, error) in
81
101
guard let data = data, error == nil else { return }
82
102
do {
83
103
let jsonObj = try JSONSerialization . jsonObject ( with: data, options: . mutableContainers)
0 commit comments