Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios swift PUT API调用缺少参数_Ios_Json_Swift_Api - Fatal编程技术网

Ios swift PUT API调用缺少参数

Ios swift PUT API调用缺少参数,ios,json,swift,api,Ios,Json,Swift,Api,我不断收到一个错误代码400:缺少参数 我认为问题在于我发送数据的格式 API文件: 我相信呼叫需要的数据是名字、姓氏和电子邮件,我是否需要将其他字段发送为零,或者可以将其留空 API调用 /// Send Lead to IDX Broker /// /// - Parameter lead: new Lead data fields /// - Returns: configs for URL Session class func putLead(lead: String) -> UR

我不断收到一个错误代码400:缺少参数 我认为问题在于我发送数据的格式

API文件:

我相信呼叫需要的数据是名字、姓氏和电子邮件,我是否需要将其他字段发送为零,或者可以将其留空

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。如果第一个和最后一个都通过了,那么它将以未收到的电子邮件进行响应


在测试代码时,当我没有在postString中使用lead()时,我能够得到预期的字符串。

postString应该是这样的:
let postString=“firstName=\(firstName.text!)&lastName=\(lastName.text!)&email=\(email.text!)”
,阅读API文档。我尝试了一下,仍然得到了一个[状态代码:400-所需参数丢失或无效。]总之,类似于lead(…)不是
应用程序/x-www-form-urlencoded
的有效格式。您可能缺少一些其他标题字段,或者
firstName
lastName
email
三个字段中的任何一个都可能包含无效数据。只需阅读API文档即可。(您应该使用
.urlQueryAllowed
而不是
.urlHostAllowed
,但如果这三个字段具有有效数据,则不会导致问题。)