I need to convert the following curl call of Microsoft Azure Cloud to Swift:
curl -X POST "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=zh-Hans&toScript=Latn" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Content-Type: application/json; charset=UTF-8" -d "[{'Text':'Hello, what is your name?'}]"
I need to convert a JSON call of curl to Swift. The problem is I have no experience with JSON calls and don't know how to include headers, a body and parameters. So I tried the following code:
let apiKey = "..."
let location = "..."
class AzureManager {
    
    static let shared = AzureManager()
    
    func makeRequest(json: [String: Any], completion: @escaping (String)->()) {
        
        guard let url = URL(string: "https://api.cognitive.microsofttranslator.com/translate"),
              let payload = try? JSONSerialization.data(withJSONObject: json) else {
            return
        }
        
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.addValue(apiKey, forHTTPHeaderField: "Ocp-Apim-Subscription-Key")
        request.addValue(location, forHTTPHeaderField: "Ocp-Apim-Subscription-Region")
        request.addValue("application/json", forHTTPHeaderField: "Content-type")
        request.addValue("NaiVVl3DEFG3jdE5DE1NFAA6EABC", forHTTPHeaderField: "X-ClientTraceId")
        request.httpBody = payload
        URLSession.shared.dataTask(with: request) { (data, response, error) in
            guard error == nil else { completion("Error::\(String(describing: error?.localizedDescription))"); return }
            guard let data = data else { completion("Error::Empty data"); return }
            
            let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
            if let json = json,
               let choices = json["translations"] as? [String: Any],
               let str = choices["text"] as? String {
                completion (str)
            } else {
                completion("Error::nothing returned")
            }
            
        }.resume()
    }
}
And then call it like this:
let jsonPayload = [
           "api-version": "3.0",
           "from": "en",
           "to": "it",
           "text": "Hello"
           ] as [String : Any]
   
AzureManager.shared.makeRequest(json: jsonPayload) { [weak self] (str) in
     DispatchQueue.main.async {
           print(str)
     }
}
Well, the errors I get are:
    Optional(["error": {
        code = 400074;
        message = "The body of the request is not valid JSON.";
    }])
And:
Error::nothing returned
Here are the Docs of Microsoft Azure Translator: https://learn.microsoft.com/de-de/azure/cognitive-services/translator/reference/v3-0-translate
 
    