我在Firebase上托管了一个Cloud Function,我正尝试从Swift应用程序调用它。当我使用以下脚本从命令行调用它时,它可以正常工作,但是当我从swift调用它时,解析JSON数据存在一些问题。
命令行(完美运行)
time curl -v -X POST -d '{"url":"https://cdn.bmstores.co.uk/images/hpcProductImage/imgFull/303441-Volvic-6x500ml-Naural-Mineral-Water1.jpg"}' -H "Content-type: application/json" https://us-central1-themagicfactory-5cf7a.cloudfunctions.net/fastMatches
快速代码(无效)
functions.httpsCallable("fastMatches").call("{\"url\":\"https://cdn.bmstores.co.uk/images/hpcProductImage/imgFull/303441-Volvic-6x500ml-Naural-Mineral-Water1.jpg\"}", completion: {(result,error) in
if let error = error{
print("An error occurred while calling the test function: \(error)" )
}
print("Results from test cloud function: \(result)")
})
Python伪(云函数)
def fastMatches(request):
print(request)
req = urllib.request.urlopen(request.json["url"])
从Swift调用时请求很奇怪
编辑:即使从手动HTTPs请求中调用,我也得到同样奇怪的结果。
let json: [String: String] = ["url": "https://cdn.bmstores.co.uk/images/hpcProductImage/imgFull/303441-Volvic-6x500ml-Naural-Mineral-Water1.jpg"]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url = URL(string: "https://us-central1-themagicfactory-5cf7a.cloudfunctions.net/fastMatches")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
print(data,response,error)
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
}
task.resume()