这是我使用python中的令牌访问http请求的方式,该方法没有问题。
import requests
import ast
session = requests.Session()
session.cookies.set('auth_token', args.token)
if not session.put("https://SomeWebAddress/audio/devices/1/volume", data=str(volume)).status_code == 200
print("Error during sending the command")
在Swift 4.2,iOS 13.0中,我正在尝试
let url:URL = URL(string: "https://SomeWebAddress/audio/devices/1/volume")!
let session:URLSession = URLSession.shared
session.configuration.httpShouldSetCookies = true
var request:URLRequest = URLRequest(url: url)
request.httpShouldHandleCookies = true
request.setValue(TOKEN, forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
request.httpBody = String(volume).data
以及
request.httpBody = String(volume) as Data
但最终会出现类似错误
无法强制将'String'类型的值转换为'Data'类型
我在做什么错?
我建议对Double
使用这样的解决方案
request.httpBody = String(volume).data(using: .utf8)
音量转换为Data
就这样