Created
August 8, 2022 11:28
-
-
Save ghwn/5941c063180317389bb053c8b2fc11d3 to your computer and use it in GitHub Desktop.
post 등록 예시
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
import Foundation | |
#if canImport(FoundationNetworking) | |
import FoundationNetworking | |
#endif | |
var semaphore = DispatchSemaphore (value: 0) | |
let parameters = [ | |
[ | |
"key": "title", | |
"value": "콜라 공동구매합니다", | |
"type": "text" | |
], | |
[ | |
"key": "unit_price", | |
"value": "1500", | |
"type": "text" | |
], | |
[ | |
"key": "quantity", | |
"value": "30", | |
"type": "text" | |
], | |
[ | |
"key": "description", | |
"value": "좋은 가격에 콜라 같이 구매하실 분~", | |
"type": "text" | |
], | |
[ | |
"key": "min_participants", | |
"value": "10", | |
"type": "text" | |
], | |
[ | |
"key": "max_participants", | |
"value": "50", | |
"type": "text" | |
], | |
[ | |
"key": "product_url", | |
"value": "https://example.com/items/coke", | |
"type": "text" | |
], | |
[ | |
"key": "trade_type", | |
"value": "DIRECT", | |
"type": "text" | |
], | |
[ | |
"key": "waited_until", | |
"value": "2022-08-05", | |
"type": "text" | |
], | |
[ | |
"key": "images", | |
"src": "/Users/jihwanlim/Downloads/coke.jpeg", | |
"type": "file" | |
], | |
[ | |
"key": "images", | |
"src": "/Users/jihwanlim/Downloads/coke-in-cup.jpeg", | |
"type": "file" | |
], | |
[ | |
"key": "images", | |
"src": "/Users/jihwanlim/Downloads/Lenna.png", | |
"type": "file" | |
]] as [[String : Any]] | |
let boundary = "Boundary-\(UUID().uuidString)" | |
var body = "" | |
var error: Error? = nil | |
for param in parameters { | |
if param["disabled"] == nil { | |
let paramName = param["key"]! | |
body += "--\(boundary)\r\n" | |
body += "Content-Disposition:form-data; name=\"\(paramName)\"" | |
if param["contentType"] != nil { | |
body += "\r\nContent-Type: \(param["contentType"] as! String)" | |
} | |
let paramType = param["type"] as! String | |
if paramType == "text" { | |
let paramValue = param["value"] as! String | |
body += "\r\n\r\n\(paramValue)\r\n" | |
} else { | |
let paramSrc = param["src"] as! String | |
let fileData = try NSData(contentsOfFile:paramSrc, options:[]) as Data | |
let fileContent = String(data: fileData, encoding: .utf8)! | |
body += "; filename=\"\(paramSrc)\"\r\n" | |
+ "Content-Type: \"content-type header\"\r\n\r\n\(fileContent)\r\n" | |
} | |
} | |
} | |
body += "--\(boundary)--\r\n"; | |
let postData = body.data(using: .utf8) | |
var request = URLRequest(url: URL(string: "http://localhost:8080/api/v1/posts/")!,timeoutInterval: Double.infinity) | |
request.addValue("Token 6984e02f99d1d82baea7ec584801799bc432c9d1", forHTTPHeaderField: "Authorization") | |
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") | |
request.httpMethod = "POST" | |
request.httpBody = postData | |
let task = URLSession.shared.dataTask(with: request) { data, response, error in | |
guard let data = data else { | |
print(String(describing: error)) | |
semaphore.signal() | |
return | |
} | |
print(String(data: data, encoding: .utf8)!) | |
semaphore.signal() | |
} | |
task.resume() | |
semaphore.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment