Last active
June 13, 2024 04:38
-
-
Save benigumocom/3704fba239fa8395ffbe0d20cf43d00b to your computer and use it in GitHub Desktop.
【Swift】URLSession メソッドの使い分け 👉 https://android.benigumo.com/20240613/urlsession/
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
let url = URL(string: "https://httpbin.org/anything/{anything}")! | |
let session = URLSession.shared | |
Task { | |
let (data, response) = try await URLSession.shared.data(from: url) | |
print((response as! HTTPURLResponse).statusCode) | |
print(String(data: data, encoding: .utf8)!) | |
// 200 | |
// { | |
// "args": {}, | |
// "data": "", | |
// "files": {}, | |
// "form": {}, | |
// "headers": { | |
// "Accept": "*/*", | |
// "Accept-Encoding": "gzip, deflate, br", | |
// "Accept-Language": "en-US,en;q=0.9", | |
// "Host": "httpbin.org", | |
// "User-Agent": "Sample/1 CFNetwork/1496.0.7 Darwin/23.5.0", | |
// }, | |
// "json": null, | |
// "method": "GET", | |
// "url": "https://httpbin.org/anything/{anything}" | |
// } | |
// let (data, response) = try await session.download(from: url) | |
// print((response as! HTTPURLResponse).statusCode) | |
// print(data.shortPath()) // URL | |
// print(try! String(contentsOf: data)) | |
// 200 | |
// /HOME/tmp/CFNetworkDownload_5NsqDg.tmp | |
// { | |
// "args": {}, | |
// "data": "", | |
// "files": {}, | |
// "form": {}, | |
// "headers": { | |
// "Accept": "*/*", | |
// "Accept-Encoding": "gzip, deflate, br", | |
// "Accept-Language": "en-US,en;q=0.9", | |
// "Host": "httpbin.org", | |
// "User-Agent": "Sample/1 CFNetwork/1496.0.7 Darwin/23.5.0", | |
// }, | |
// "json": null, | |
// "method": "GET", | |
// "url": "https://httpbin.org/anything/{anything}" | |
// } | |
// var request = URLRequest(url: url) | |
// request.httpMethod = "POST" | |
// let file = URL.temporaryDirectory.appending(component: "CFNetworkDownload_5NsqDg.tmp") | |
// let (data, response) = try await session.upload(for: request, fromFile: file) | |
// print(String(data: data, encoding: .utf8)!) | |
// | |
// { | |
// "args": {}, | |
// "data": "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\": \"gzip, deflate, br\", \n \"Accept-Language\": \"en-US,en;q=0.9\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"Sample/1 CFNetwork/1496.0.7 Darwin/23.5.0\", \n }, \n \"json\": null, \n \"method\": \"GET\", \n \"url\": \"https://httpbin.org/anything/{anything}\"\n}\n", | |
// "files": {}, | |
// "form": {}, | |
// "headers": { | |
// "Accept": "*/*", | |
// "Accept-Encoding": "gzip, deflate, br", | |
// "Accept-Language": "en-US,en;q=0.9", | |
// "Content-Length": "469", // * | |
// "Content-Type": "application/octet-stream", // * | |
// "Host": "httpbin.org", | |
// "Upload-Draft-Interop-Version": "3", // * | |
// "Upload-Incomplete": "?0", // * | |
// "User-Agent": "Sample/1 CFNetwork/1496.0.7 Darwin/23.5.0", | |
// }, | |
// "json": { | |
// "args": {}, | |
// "data": "", | |
// "files": {}, | |
// "form": {}, | |
// "headers": { | |
// "Accept": "*/*", | |
// "Accept-Encoding": "gzip, deflate, br", | |
// "Accept-Language": "en-US,en;q=0.9", | |
// "Host": "httpbin.org", | |
// "User-Agent": "Sample/1 CFNetwork/1496.0.7 Darwin/23.5.0", | |
// }, | |
// "json": null, | |
// "method": "GET", | |
// }, | |
// "method": "POST", | |
// "url": "https://httpbin.org/anything/{anything}" | |
// } | |
// let (asyncBytes, response) = try await session.bytes(from: url) | |
// var data = Data() | |
// for try await byte in asyncBytes { | |
// data.append(byte) | |
// } | |
// print(String(data: data, encoding: .utf8)!) | |
// { | |
// "args": {}, | |
// "data": "", | |
// "files": {}, | |
// "form": {}, | |
// "headers": { | |
// "Accept": "*/*", | |
// "Accept-Encoding": "gzip, deflate, br", | |
// "Accept-Language": "en-US,en;q=0.9", | |
// "Host": "httpbin.org", | |
// "User-Agent": "Sample/1 CFNetwork/1496.0.7 Darwin/23.5.0", | |
// }, | |
// "json": null, | |
// "method": "GET", | |
// "url": "https://httpbin.org/anything/{anything}" | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment