Alamofire POST 请求,正文中有一个简单的字符串作为参数,标头仍然显示缺少的参数值

Posted

技术标签:

【中文标题】Alamofire POST 请求,正文中有一个简单的字符串作为参数,标头仍然显示缺少的参数值【英文标题】:Alamofire POST request with a simple string in body as parameter and header still showing missing parameters values 【发布时间】:2019-01-20 06:33:53 【问题描述】:

即使传递了parameters 和头文件,从服务器获取响应

我也试过encoding: JSONEncoding.defaultJSONEncoding.JSON

let URL_GET_DATA = http://abcdef.com:4207/base/signin

let headers = [
            "Authorization": "Bearer 5baa9fd27862adc397033f1ff27d73b0c2318144",
            "Content-Type":  "application/x-www-form-urlencoded"
        ]

         let parameters: Parameters = [
            "Start": "1",
            "end" : "11"
        ]
Alamofire.request(URL_GET_DATA, method: .post, parameters: parameters as! Parameters, headers: headers).responseJSON  response in
            print("Request  \(response.request)")

            print("RESPONSE \(response.result.value)")
            print("RESPONSE \(response.result)")
            print("RESPONSE \(response)")


            switch response.result 
            case .success:
                print(response)
                let dictVal = response.result.value
                let dictMain:NSDictionary = dictVal as! NSDictionary
                let statusCode = dictMain.value(forKey: "statusCode") as! Int
                let statusMsg = dictMain.value(forKey: "statusMsg") as! String
                print(statusMsg)
                print(statusCode)
                                //if the auth is sucessfull then
                if(statusCode == 0)
                
                    let dictVal1 = dictMain.value(forKey: "data") as! NSDictionary
                    //get the responce code or Token From the server
                    let statusCode1 = dictVal1.value(forKey: "accessToken") as! String
                    let statusMsg1 = dictVal1.value(forKey: "refreshTokenExpiresAt") as! String

                    //Store data Locally to create session
                    UserDefaults.standard.set(false, forKey: "userSession")
                    print(statusCode1)
                    print(statusMsg1)

                    //dissmiss the screen and go to the home page
                    //self.dismiss(animated: true, completion: nil)


                
                    // if the auth is Failre
                else if(statusCode == 1)
                
                    self.displayMyAlertMessage(userMessage: "Please Enter The Correct Credencial, Username or pasword is incorrect ")
                
                case .failure(let error):
                print(error)
                self.displayMyAlertMessage(userMessage: "You are not Connected to the Internet Please Check The connection. \(error)")
            
        

    

【问题讨论】:

您可以执行此操作 (***.com/questions/53637437/alamofire-with-d/…) 请求真正的样子。并检查是否因为您告诉参数是x-www-form-urlencoded,例如情况确实如此(或缺少其他内容)。 【参考方案1】:
  let URL_GET_DATA = "http://abcdef.com:4207/base/signin"


    let parameters = [
        "email_id": userEmail, //email
        "password": userPassword //password
    ]
    var statusCode: Int = 0
    print(parameters)
    //Almofire Code
    Alamofire.request(URL_GET_DATA, method: .post, parameters: parameters).responseJSON  response in
        print(response)

        switch response.result 
        case .success:
            print(response)
            let dictVal = response.result.value
            let dictMain:NSDictionary = dictVal as! NSDictionary
            let statusCode = dictMain.value(forKey: "statusCode") as! Int
            let statusMsg = dictMain.value(forKey: "statusMsg") as! String
            print(statusMsg)
            print(statusCode)

            //if the auth is sucessfull then
            if(statusCode == 0)
            
                let dictVal1 = dictMain.value(forKey: "data") as! NSDictionary
                //get the responce code or Token From the server
                var statusCode1="Bearer "
                statusCode1 += dictVal1.value(forKey: "accessToken") as! String
                //                    let statusCode1 = dictVal1.value(forKey: "accessToken") as! String
                let statusMsg1 = dictVal1.value(forKey: "refreshTokenExpiresAt") as! String

                //Store data Locally to create session
                UserDefaults.standard.set(false, forKey: "userSession")
                UserDefaults.standard.set(statusCode1, forKey: "accessToken")

                //to pass the email to the Header file
                UserDefaults.standard.set(userEmail, forKey: "emailSession")
                let emailId = UserDefaults.standard.object(forKey: "emailSession")
                print(emailId!)
                //                    print(statusCode1)
                //                    print(statusMsg1)

                //dissmiss the screen and go to the home page
                self.dismiss(animated: true, completion: nil)


            

                // if the auth is Failre
            else if(statusCode == 1)
            
                self.displayMyAlertMessage(userMessage: "Please Enter The Correct Credencial, Username or pasword is incorrect ")

            
        case .failure(let error):
            print(error)
            self.displayMyAlertMessage(userMessage: "You are not Connected to the Internet Please Check The connection. \(error)")
        
    

**这是可以工作的代码**

【讨论】:

【参考方案2】:

返回的错误不是缺少参数,而是授权错误,我想您的服务器在实际请求之前需要 OAuth 或其他授权。

【讨论】:

相同的 API 已经在 android 上运行,我们正在使用 OAuth 身份验证,但出现与上面列出的相同的错误

以上是关于Alamofire POST 请求,正文中有一个简单的字符串作为参数,标头仍然显示缺少的参数值的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Alamofire 4.0 中仅使用参数和正文在 swift 中使用 POST 方法发送请求?

发送带有参数的 Alamofire 请求以及 HTTP 正文中的图像

快递中的请求正文未定义?

使用 alamofire 将带有 JSON 对象和查询参数的 POST 请求发送到 REST Web 服务

如何在 Swift 3 中使用 Alamofire 在 POST 请求中将给定的 JSON 作为参数发送?

Swift:带有 json 正文的 alamofire 帖子