swift PUT API调用缺less参数

我继续得到一个错误代码400:缺less参数我相信问题是我发送数据的格式

API DOC: http : //middleware.idxbroker.com/docs/api/methods/index.html#api-Leads-putLead

我相信所需的数据需要的是名字,姓氏和电子邮件,我是否需要发送其他领域为零或我可以让他们空白?

API调用

/// Send Lead to IDX Broker /// /// - Parameter lead: new Lead data fields /// - Returns: configs for URL Session class func putLead(lead: String) -> URLRequest { let urlString = "https://api.idxbroker.com/leads/lead" let url = NSURL(string: urlString) var downloadTask = URLRequest(url: (url as URL?)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20) /******************** Add Headers required for API CALL *************************************/ downloadTask.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") downloadTask.setValue(APICalls.getAccessKey(), forHTTPHeaderField: "accesskey") downloadTask.setValue("json", forHTTPHeaderField: "outputtype") downloadTask.httpMethod = "PUT" downloadTask.httpBody = lead.data(using: .utf8) /******************** End Headers required for API CALL *************************************/ return downloadTask } 

数据发送到函数

  /// Create lead from text fields let postString = "lead(firstName=\(String(describing: firstName.text!))&lastName=\(String(describing: lastName.text!))&=email=\(String(describing: Email.text!)))" /// URL Encoding for put API let escapedString = postString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) print(escapedString!) print(escapedString?.data(using: .utf8) ?? "Error") /// API Call with passing json String let downloadTask = APICalls.putLead(lead: escapedString!) URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in /// Status Returned from API CALL if let httpResponse = response as? HTTPURLResponse { print("statusCode: \(httpResponse.statusCode)") } }).resume() /******** End URL Session **********/ 

400意味着API没有越来越多或者更多你传递的参数。 如果API收到firstName,那么它会回应一个错误,指出没有收到lastName。 如果拳头和最后通过,那么它会回应没有收到的电子邮件。

testing你的代码当我在postString中不使用lead()的时候,我能够得到预期的string。