如何在Swift中使用URLSession制作特定的Curl请求

huangapple go评论70阅读模式
英文:

How to make specific curl request in swift using URLSession

问题

如何看起来: `curl https://token-vault.repeatphotos.xyz/token -H "Authorization: e11f18b4-5015-45ad-8276-18269a7bf047""` 响应是字符串值。因此,主要问题是如何在Swift中发出请求并获取该值?
我尝试过类似这样的代码:

```swift
func changeApiKey() {
    let url = URL(string: "https://token-vault.repeatphotos.xyz/token")

    var request = URLRequest(url: url!)

    request.httpMethod = "GET"
    request.addValue("e11f18b4-5015-45ad-8276-18269a7bf047", forHTTPHeaderField: "Authorization")

    let task = URLSession.shared.dataTask(with: url!) { data, response, error in
        guard error == nil else {
            print(error!)
            return
        }
        guard let data = data else {
            print("Data is empty")
            return
        }

        let json = try! JSONSerialization.jsonObject(with: data, options: [])
        print(json)
    }
    task.resume()
}

<details>
<summary>英文:</summary>

How it looks: `curl https://token-vault.repeatphotos.xyz/token -H &quot;Authorization: e11f18b4-5015-45ad-8276-18269a7bf047&quot;&quot;` Response is string value. So the main question is, how to make a request and get value from this in swift? 
I&#39;ve tried something like this: 

    func changeApiKey() {
            let url = URL(string: &quot;https://token-vault.repeatphotos.xyz/token&quot;)
    
            var request = URLRequest(url: url!)
    
            request.httpMethod = &quot;GET&quot;
            request.addValue(&quot;e11f18b4-5015-45ad-8276-18269a7bf047&quot;, forHTTPHeaderField: &quot;Authorization&quot;)
    
            let task = URLSession.shared.dataTask(with: url!) { data, response, error in
                guard error == nil else {
                    print(error!)
                    return
                }
                guard let data = data else {
                    print(&quot;Data is empty&quot;)
                    return
                }
    
                let json = try! JSONSerialization.jsonObject(with: data, options: [])
                print(json)
            }
            task.resume()
        }

</details>


# 答案1
**得分**: 0

尝试像这样做,对我有效,它提供了 "newty-token":

```swift
func changeApiKey() {
    if let url = URL(string: "https://token-vault.repeatphotos.xyz/token") {
        
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        request.setValue("text/plain",forHTTPHeaderField: "Accept")
        request.setValue("e11f18b4-5015-45ad-8276-18269a7bf047", forHTTPHeaderField: "Authorization")
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard error == nil else {
                print(error!)
                return
            }
            guard let data = data else {
                print("Data is empty")
                return
            }
            
            let result = String(data: data, encoding: .utf8)
            print("result: \(result)")
            
        }
        task.resume()
    }
}
英文:

try something like this, works for me, it gives "newty-token"

func changeApiKey() {
    if let url = URL(string: &quot;https://token-vault.repeatphotos.xyz/token&quot;) {
        
        var request = URLRequest(url: url)
        request.httpMethod = &quot;GET&quot;
        request.setValue(&quot;text/plain&quot;,forHTTPHeaderField: &quot;Accept&quot;)
        request.setValue(&quot;e11f18b4-5015-45ad-8276-18269a7bf047&quot;, forHTTPHeaderField: &quot;Authorization&quot;)
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard error == nil else {
                print(error!)
                return
            }
            guard let data = data else {
                print(&quot;Data is empty&quot;)
                return
            }
            
            let result = String(data: data, encoding: .utf8)
            print(&quot;result: \(result)&quot;)
            
        }
        task.resume()
    }
}

huangapple
  • 本文由 发表于 2023年3月31日 19:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定