Swift API调用中未传递参数

sf6xfgos  于 2023-06-21  发布在  Swift
关注(0)|答案(1)|浏览(121)

我们正在传递电子邮件和密码,但它不会在API调用中发送,
代码

Button(action: { Task{ func apiCall() { let endpoint = "https://new.demozab.com/cryptoco/mobileapi/public/api/login" guard let url = URL(string: endpoint) else { return } var request = URLRequest(url: url) request.addValue("application/JSON", forHTTPHeaderField: "Content-Type") request.httpMethod = "POST" let parameters: [String: Any Hashable] = [ "email":Email, "password":Password, "device_id" : "abcdef", "device_type" : "ios", "ip" : "0.0.0.0" ] do { request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .fragmentsAllowed) } catch let error { print(error.localizedDescription) return } let task = URLSession.shared.dataTask(with: request){ (data , _ , error ) in guard let data = data ,error == nil else{ return } do{ let response = try JSONSerialization.jsonObject(with: data , options: .fragmentsAllowed) print("Success \(response)") } catch{ print(error) } } task.resume() } apiCall() } }, ```

输出:

Success {
    message = "The email field is required.";
    response = "";
    status = 0;
}
ni65a41a

ni65a41a1#

您的apiCall()函数工作正常,并正确地将参数传递到URLRequest的主体中。
您很可能需要重新检查端点的文档。我用你提供的相同参数(在Postman中,使用测试电子邮件和测试密码)点击URL,我从服务器收到了相同的错误响应。

相关问题