Skip to content

Commit b0f3835

Browse files
author
shangkun
committed
Support POST http request.
1 parent dbcb018 commit b0f3835

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

SKGenerateModelTool.dmg

923 Bytes
Binary file not shown.

SKGenerateModelTool/SKCodeBuilder.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,5 +552,16 @@ extension String {
552552
}
553553
return nil
554554
}
555+
556+
func nsrangeOf(str:String) -> NSRange {
557+
let nsString = NSString.init(string: self)
558+
return nsString.range(of: str)
559+
}
560+
561+
func nsRange(from range: Range<String.Index>) -> NSRange {
562+
let start: Int = self.distance(from: startIndex, to: range.lowerBound)
563+
let end: Int = self.distance(from: startIndex, to: range.upperBound)
564+
return NSMakeRange(start, end - start)
565+
}
555566
}
556567

SKGenerateModelTool/ViewController.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
6767

6868
// MARK: - IBAction
6969

70-
/// GET request URL
70+
/// GET / POST request URL
7171

7272
@IBAction func requestURLBtnClicked(_ sender: NSButton) {
7373

@@ -77,7 +77,27 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
7777
print("encode URL = \(urlTF.stringValue)")
7878
UserDefaults.standard.setValue(urlString, forKey: LastInputURLCacheKey)
7979
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
81101
guard let data = data, error == nil else { return }
82102
do {
83103
let jsonObj = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)

0 commit comments

Comments
 (0)