使用JSON数据从Swift调用云函数

使用JSON数据从Swift调用云函数,swift,firebase,google-cloud-functions,Swift,Firebase,Google Cloud Functions,我在Firebase上有一个云函数,我正试图通过Swift应用程序调用它。当我使用下面的脚本从命令行调用它时,它工作得很好,但是当我从swift调用它时,解析JSON数据会出现一些问题 命令行(工作正常) Swift代码(不起作用) Python伪(云函数) 从Swift呼叫时请求异常 编辑:即使我通过手动HTTPs请求调用,也会得到同样奇怪的结果 let json: [String: String] = ["url": "https://cdn.bmstores.co.uk/images/h

我在Firebase上有一个云函数,我正试图通过Swift应用程序调用它。当我使用下面的脚本从命令行调用它时,它工作得很好,但是当我从swift调用它时,解析JSON数据会出现一些问题

命令行(工作正常)

Swift代码(不起作用)

Python伪(云函数)

从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()

我认为这是因为
/
已使用
`转义,而您的服务器无法管理它。您可以添加
let jsonStr=String(数据:jsonData!,编码:.utf8);印刷品(jsonStr);print(jsonStr!)`对于您的最后一个示例,您将看到非常感谢您提供的这一关键信息!!!使用
request.setValue(“application/json”,forHTTPHeaderField:“Content Type”)
能够解决这个问题,我想这是因为
/
已经用
`转义,而您的服务器无法管理它。您可以添加
let jsonStr=String(数据:jsonData!,编码:.utf8);印刷品(jsonStr);print(jsonStr!)`对于您的最后一个示例,您将看到非常感谢您提供的这一关键信息!!!能够使用
request.setValue(“application/json”,forHTTPHeaderField:“Content Type”)
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)")
})
def fastMatches(request):
    print(request)
    req = urllib.request.urlopen(request.json["url"])
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()