Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norio-nomura/73c3a12db6a9d615e239954259ff89cf to your computer and use it in GitHub Desktop.
Save norio-nomura/73c3a12db6a9d615e239954259ff89cf to your computer and use it in GitHub Desktop.
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