Alamofire发布请求在Swift中返回404

时间:2017-11-15 19:05:14

标签: swift3 alamofire

我将通过alamofire发布请求。我可以跟Postman发帖请求。响应返回true(201代码)。但是,当我尝试使用Alamofire返回404代码时。怎么了?

我的代码:

{-# LANGUAGE LambdaCase #-}
catMaybes :: [Maybe a] -> [a]
catMaybes xs = foldl((++) \case Nothing -> []; Just y -> y) [] xs

返回此消息:

     let headers: HTTPHeaders = [
    "Authorization": "Basic xxxxxxxxxxxx",
    "content-type": "application/json"
     ]
        let parameters:[String:Any] = [
        "xxx":123,
        "yyy":"test",
        "zzz":"iphone"
        ]

    Alamofire.request(myUrl, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response -> Void in

        switch response.result {
        case .success:
            print(response.response?.statusCode)

            break
        case .failure(let error):

            print(error)
        }
    }

}

2 个答案:

答案 0 :(得分:0)

尝试这个希望它可以帮助你!!

Public static void main(String[] args){
    printLettersForward('D');
    System.out.println();
    printLettersForward('d');
    System.out.println()
}


public static void printLettersForward(char c) {  

    if (c >= 'a'||c >= 'z') {
        printLettersForward((char) (c - 1));
        System.out.print(c+" ");    

    }else if(c >= 'A'||c >= 'Z') {
        printLettersForward((char) (c - 1));
        System.out.print(c+" ");    

    }else {
        System.out.println();
    }


}

答案 1 :(得分:0)

您可以尝试使用此方法来使用Alamofire,因为这对我有用。

let headers = [
               "Accept": "application/json",
               "Authorization" : "Authorization: Bearer ", //if any
               "Cookie" : "Cookie" //if any
              ]

let parameterDict: NSDictionary = NSDictionary.init(objects: [nameTextField.text!, reportTextView.text!], forKeys: ["Name" as NSCopying,"Message" as NSCopying])

Alamofire.request("API",method: .post, parameters: parameterDict as? [String : AnyObject] , encoding:JSONEncoding.default, headers:headers) .responseJSON {  response in switch response.result {

     case .success(let JSON):
         print("Success with JSON: \(JSON)")
         let response = JSON as! NSDictionary

     case .failure(let error):
         print("Request failed with error: \(error)")


         }
    }