Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Dispatch
import Foundation
var setCookieRequest = URLRequest(url: URL(string: "http://httpbin.org/cookies/set?freeform=test")!)
let setCookieTask = URLSession.shared.dataTask(with: setCookieRequest) { _, _, error in
guard error == nil else { fatalError("failed to set cookie!") }
var request = URLRequest(url: URL(string: "http://httpbin.org/post")!)
request.httpMethod = "POST"
request.httpBody = "{\"key\":\"value\"}".data(using: .utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
print("request.httpBody: \(String(describing: request.httpBody))")
let postTask = URLSession.shared.dataTask(with: request) { data, response, error in
print("-- response body begin")
print(data.flatMap { String(data: $0, encoding: .utf8) } ?? "nil")
print("-- response body end")
exit(0)
}
print("postTask.originalRequest.httpBody: \(String(describing: postTask.originalRequest?.httpBody))")
postTask.resume()
}
setCookieTask.resume()
dispatchMain()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment