我不断收到错误代码400:缺少参数 我认为问题是我在
中发送数据的格式API DOC:http://middleware.idxbroker.com/docs/api/methods/index.html#api-Leads-putLead
我认为呼叫所需的数据是名字,姓氏和电子邮件,我是否需要将其他字段作为nil发送,还是可以将其留空?
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 **********/
答案 0 :(得分:0)
400表示API没有开启,或者您传递的参数更多。如果API收到firstName,那么它会回复一个错误,指出没有收到lastName。如果第一个和最后一个通过,那么它将回复未收到的电子邮件。
测试你的代码当我没有在postString中使用lead()时,我能够获得预期的字符串。