快速的PUT API调用缺少参数 [英] swift PUT API Call missing parameters
问题描述
我一直收到错误代码400:缺少参数 我相信问题在于我发送数据的格式
I Keep getting an Error Code 400: Missing parameters I believe the problem is the format i am sending the data in
API DOC: http://middleware. idxbroker.com/docs/api/methods/index.html#api-Leads-putLead
我相信通话所需的数据是名字,姓氏和电子邮件,我是否需要将其他字段发送为nil还是可以将它们留空?
I believe the required data the call needs is the First Name, Last Name, and email, do I need to send the other field as nil or can i leave them blank?
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.如果第一拳和最后一支拳通过了,它将以未收到的电子邮件作为响应.
400 means the API is not getting on or more of the parameters you are passing. If the API received firstName then it would respond with an error stating that lastName was not received. If fist and last are passed then it would respond with email not received.
测试您的代码,当我在postString中不使用lead()时,我能够获得预期的字符串.
Testing your code I was able to get the expected string when I didn't use lead() in postString.
这篇关于快速的PUT API调用缺少参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!