From f30e998112c0f9b710bd4899bab1d30894480be9 Mon Sep 17 00:00:00 2001 From: zunda <47569369+zunda-pixel@users.noreply.github.com> Date: Fri, 12 Apr 2024 01:14:39 +0900 Subject: [PATCH] Add query logic in Swift (#227) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 🧰 Changes - Add query logic in Swift - URLQueryItem has url encode logic ## 🧬 QA & Testing I fixed test code in `/httpsnippet/src/targets/swift/urlsession/fixtures/` `npm run test` --- src/targets/swift/urlsession/client.ts | 35 +++++++++++++++++-- .../fixtures/application-form-encoded.swift | 5 +-- .../fixtures/application-json.swift | 5 +-- .../swift/urlsession/fixtures/cookies.swift | 5 +-- .../urlsession/fixtures/custom-method.swift | 5 +-- .../swift/urlsession/fixtures/full.swift | 14 ++++++-- .../swift/urlsession/fixtures/headers.swift | 5 +-- .../urlsession/fixtures/http-insecure.swift | 5 +-- .../urlsession/fixtures/indent-option.swift | 5 +-- .../urlsession/fixtures/json-null-value.swift | 5 +-- .../fixtures/jsonObj-multiline.swift | 5 +-- .../fixtures/jsonObj-null-value.swift | 5 +-- .../urlsession/fixtures/multipart-data.swift | 5 +-- .../urlsession/fixtures/multipart-file.swift | 5 +-- .../multipart-form-data-no-params.swift | 5 +-- .../fixtures/multipart-form-data.swift | 5 +-- .../swift/urlsession/fixtures/nested.swift | 13 +++++-- .../fixtures/postdata-malformed.swift | 5 +-- .../urlsession/fixtures/pretty-option.swift | 14 ++++++-- .../urlsession/fixtures/query-encoded.swift | 12 +++++-- .../swift/urlsession/fixtures/query.swift | 14 ++++++-- .../swift/urlsession/fixtures/short.swift | 5 +-- .../urlsession/fixtures/text-plain.swift | 5 +-- .../urlsession/fixtures/timeout-option.swift | 5 +-- 24 files changed, 143 insertions(+), 49 deletions(-) diff --git a/src/targets/swift/urlsession/client.ts b/src/targets/swift/urlsession/client.ts index e980cf47..466f96b8 100644 --- a/src/targets/swift/urlsession/client.ts +++ b/src/targets/swift/urlsession/client.ts @@ -25,7 +25,7 @@ export const urlsession: Client = { description: "Foundation's URLSession request", extname: '.swift', }, - convert: ({ allHeaders, postData, fullUrl, method }, options) => { + convert: ({ allHeaders, postData, uriObj, queryObj, method }, options) => { const opts = { indent: ' ', pretty: true, @@ -122,7 +122,36 @@ export const urlsession: Client = { blank(); - push(`var request = URLRequest(url: URL(string: "${fullUrl}")!)`); + push(`let url = URL(string: "${uriObj.href}")!`); + + const queries = queryObj ? Object.entries(queryObj) : []; + if (queries.length < 1) { + push('var request = URLRequest(url: url)'); + } else { + push('var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!'); + push('let queryItems: [URLQueryItem] = ['); + + queries.forEach(query => { + const key = query[0]; + const value = query[1]; + switch (Object.prototype.toString.call(value)) { + case '[object String]': + push(`${opts.indent}URLQueryItem(name: "${key}", value: "${value}"),`); + break; + case '[object Array]': + value.forEach(val => { + push(`${opts.indent}URLQueryItem(name: "${key}", value: "${val}"),`); + }); + break; + } + }); + push(']'); + push('components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems'); + + blank(); + push('var request = URLRequest(url: components.url!)'); + } + push(`request.httpMethod = "${method}"`); if (req.hasHeaders) { @@ -136,7 +165,7 @@ export const urlsession: Client = { blank(); // Retrieving the shared session will be less verbose than creating a new one. - push('let (data, response) = try await URLSession.shared.data(with: request)'); + push('let (data, response) = try await URLSession.shared.data(for: request)'); push('print(String(decoding: data, as: UTF8.self))'); blank(); diff --git a/src/targets/swift/urlsession/fixtures/application-form-encoded.swift b/src/targets/swift/urlsession/fixtures/application-form-encoded.swift index 5fcc2d29..2e5a5630 100644 --- a/src/targets/swift/urlsession/fixtures/application-form-encoded.swift +++ b/src/targets/swift/urlsession/fixtures/application-form-encoded.swift @@ -8,10 +8,11 @@ let headers = ["content-type": "application/x-www-form-urlencoded"] var postData = Data("foo=bar".utf8) postData.append(Data("&hello=world".utf8)) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/application-json.swift b/src/targets/swift/urlsession/fixtures/application-json.swift index 7f97c825..d2c6b3cc 100644 --- a/src/targets/swift/urlsession/fixtures/application-json.swift +++ b/src/targets/swift/urlsession/fixtures/application-json.swift @@ -15,10 +15,11 @@ let parameters = [ let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/cookies.swift b/src/targets/swift/urlsession/fixtures/cookies.swift index 483e7bbe..dd0f7cdb 100644 --- a/src/targets/swift/urlsession/fixtures/cookies.swift +++ b/src/targets/swift/urlsession/fixtures/cookies.swift @@ -5,9 +5,10 @@ import Foundation let headers = ["cookie": "foo=bar; bar=baz"] -var request = URLRequest(url: URL(string: "https://httpbin.org/cookies")!) +let url = URL(string: "https://httpbin.org/cookies")! +var request = URLRequest(url: url) request.httpMethod = "GET" request.allHTTPHeaderFields = headers -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/custom-method.swift b/src/targets/swift/urlsession/fixtures/custom-method.swift index 5bd19a3b..40b4a86f 100644 --- a/src/targets/swift/urlsession/fixtures/custom-method.swift +++ b/src/targets/swift/urlsession/fixtures/custom-method.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "PROPFIND" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/full.swift b/src/targets/swift/urlsession/fixtures/full.swift index 39cba345..7df94abd 100644 --- a/src/targets/swift/urlsession/fixtures/full.swift +++ b/src/targets/swift/urlsession/fixtures/full.swift @@ -11,10 +11,20 @@ let headers = [ let postData = Data("foo=bar".utf8) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value")!) +let url = URL(string: "https://httpbin.org/anything?key=value")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "foo", value: "bar"), + URLQueryItem(name: "foo", value: "baz"), + URLQueryItem(name: "baz", value: "abc"), + URLQueryItem(name: "key", value: "value"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/headers.swift b/src/targets/swift/urlsession/fixtures/headers.swift index 07601f12..da999ee4 100644 --- a/src/targets/swift/urlsession/fixtures/headers.swift +++ b/src/targets/swift/urlsession/fixtures/headers.swift @@ -10,9 +10,10 @@ let headers = [ "quoted-value": "\"quoted\" 'string'" ] -var request = URLRequest(url: URL(string: "https://httpbin.org/headers")!) +let url = URL(string: "https://httpbin.org/headers")! +var request = URLRequest(url: url) request.httpMethod = "GET" request.allHTTPHeaderFields = headers -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/http-insecure.swift b/src/targets/swift/urlsession/fixtures/http-insecure.swift index 42687b86..e4826673 100644 --- a/src/targets/swift/urlsession/fixtures/http-insecure.swift +++ b/src/targets/swift/urlsession/fixtures/http-insecure.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "http://httpbin.org/anything")!) +let url = URL(string: "http://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/indent-option.swift b/src/targets/swift/urlsession/fixtures/indent-option.swift index b58e4474..d7ae5274 100644 --- a/src/targets/swift/urlsession/fixtures/indent-option.swift +++ b/src/targets/swift/urlsession/fixtures/indent-option.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/json-null-value.swift b/src/targets/swift/urlsession/fixtures/json-null-value.swift index 4ba8885f..caac59fe 100644 --- a/src/targets/swift/urlsession/fixtures/json-null-value.swift +++ b/src/targets/swift/urlsession/fixtures/json-null-value.swift @@ -8,10 +8,11 @@ let parameters = ["foo": nil] as [String : Any] let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift b/src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift index 016fff7f..ed53b1ef 100644 --- a/src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift +++ b/src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift @@ -8,10 +8,11 @@ let parameters = ["foo": "bar"] as [String : Any] let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/jsonObj-null-value.swift b/src/targets/swift/urlsession/fixtures/jsonObj-null-value.swift index 4ba8885f..caac59fe 100644 --- a/src/targets/swift/urlsession/fixtures/jsonObj-null-value.swift +++ b/src/targets/swift/urlsession/fixtures/jsonObj-null-value.swift @@ -8,10 +8,11 @@ let parameters = ["foo": nil] as [String : Any] let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/multipart-data.swift b/src/targets/swift/urlsession/fixtures/multipart-data.swift index d858b314..529bf799 100644 --- a/src/targets/swift/urlsession/fixtures/multipart-data.swift +++ b/src/targets/swift/urlsession/fixtures/multipart-data.swift @@ -39,10 +39,11 @@ for param in parameters { } } -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/multipart-file.swift b/src/targets/swift/urlsession/fixtures/multipart-file.swift index af9bc9f8..ba9c867d 100644 --- a/src/targets/swift/urlsession/fixtures/multipart-file.swift +++ b/src/targets/swift/urlsession/fixtures/multipart-file.swift @@ -34,10 +34,11 @@ for param in parameters { } } -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/multipart-form-data-no-params.swift b/src/targets/swift/urlsession/fixtures/multipart-form-data-no-params.swift index 9ff1fa7b..eb2276ac 100644 --- a/src/targets/swift/urlsession/fixtures/multipart-form-data-no-params.swift +++ b/src/targets/swift/urlsession/fixtures/multipart-form-data-no-params.swift @@ -5,9 +5,10 @@ import Foundation let headers = ["Content-Type": "multipart/form-data"] -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/multipart-form-data.swift b/src/targets/swift/urlsession/fixtures/multipart-form-data.swift index 3b11e389..e9d349eb 100644 --- a/src/targets/swift/urlsession/fixtures/multipart-form-data.swift +++ b/src/targets/swift/urlsession/fixtures/multipart-form-data.swift @@ -33,10 +33,11 @@ for param in parameters { } } -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/nested.swift b/src/targets/swift/urlsession/fixtures/nested.swift index 56330a19..a67a057b 100644 --- a/src/targets/swift/urlsession/fixtures/nested.swift +++ b/src/targets/swift/urlsession/fixtures/nested.swift @@ -3,8 +3,17 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value")!) +let url = URL(string: "https://httpbin.org/anything")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "foo[bar]", value: "baz,zap"), + URLQueryItem(name: "fiz", value: "buz"), + URLQueryItem(name: "key", value: "value"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/postdata-malformed.swift b/src/targets/swift/urlsession/fixtures/postdata-malformed.swift index 71b4f4a9..29df4fd8 100644 --- a/src/targets/swift/urlsession/fixtures/postdata-malformed.swift +++ b/src/targets/swift/urlsession/fixtures/postdata-malformed.swift @@ -5,9 +5,10 @@ import Foundation let headers = ["content-type": "application/json"] -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/pretty-option.swift b/src/targets/swift/urlsession/fixtures/pretty-option.swift index 3dcf9c52..3da5fca5 100644 --- a/src/targets/swift/urlsession/fixtures/pretty-option.swift +++ b/src/targets/swift/urlsession/fixtures/pretty-option.swift @@ -7,10 +7,20 @@ let headers = ["cookie": "foo=bar; bar=baz", "accept": "application/json", "cont let postData = Data("foo=bar".utf8) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value")!) +let url = URL(string: "https://httpbin.org/anything?key=value")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "foo", value: "bar"), + URLQueryItem(name: "foo", value: "baz"), + URLQueryItem(name: "baz", value: "abc"), + URLQueryItem(name: "key", value: "value"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/query-encoded.swift b/src/targets/swift/urlsession/fixtures/query-encoded.swift index d57bd20d..a264a7d2 100644 --- a/src/targets/swift/urlsession/fixtures/query-encoded.swift +++ b/src/targets/swift/urlsession/fixtures/query-encoded.swift @@ -3,8 +3,16 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00")!) +let url = URL(string: "https://httpbin.org/anything")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "startTime", value: "2019-06-13T19%3A08%3A25.455Z"), + URLQueryItem(name: "endTime", value: "2015-09-15T14%3A00%3A12-04%3A00"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/query.swift b/src/targets/swift/urlsession/fixtures/query.swift index a8a0b801..8e5f34f2 100644 --- a/src/targets/swift/urlsession/fixtures/query.swift +++ b/src/targets/swift/urlsession/fixtures/query.swift @@ -3,8 +3,18 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value")!) +let url = URL(string: "https://httpbin.org/anything?key=value")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "foo", value: "bar"), + URLQueryItem(name: "foo", value: "baz"), + URLQueryItem(name: "baz", value: "abc"), + URLQueryItem(name: "key", value: "value"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/short.swift b/src/targets/swift/urlsession/fixtures/short.swift index b58e4474..d7ae5274 100644 --- a/src/targets/swift/urlsession/fixtures/short.swift +++ b/src/targets/swift/urlsession/fixtures/short.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/text-plain.swift b/src/targets/swift/urlsession/fixtures/text-plain.swift index 0c6b0487..72583278 100644 --- a/src/targets/swift/urlsession/fixtures/text-plain.swift +++ b/src/targets/swift/urlsession/fixtures/text-plain.swift @@ -7,10 +7,11 @@ let headers = ["content-type": "text/plain"] let postData = Data("Hello World".utf8) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/timeout-option.swift b/src/targets/swift/urlsession/fixtures/timeout-option.swift index b58e4474..d7ae5274 100644 --- a/src/targets/swift/urlsession/fixtures/timeout-option.swift +++ b/src/targets/swift/urlsession/fixtures/timeout-option.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self))