简体   繁体   English

邮递员对Alamofire的请求

[英]Postman request to Alamofire request

I'm having no issues when I make the post request with POSTMAN but when I use alamofire I have issues. 我通过POSTMAN发出发帖请求时没有问题,但是当我使用alamofire时遇到了问题。 The post still goes through on the alamofire request but the data is not received the same way. 该帖子仍按alamofire请求进行,但数据接收方式不同。 What does an alamofire request look like that's the exact same as the following postman... alamofire请求的内容与以下邮递员的情况完全相同...

在此处输入图片说明

Swift 2.x : Swift 2.x

typealias apiSuccess = (result: NSDictionary?) -> Void
typealias apiProgress = (result: NSDictionary?) -> Void // when you want to download or upload using Alamofire..
typealias apiFailure = (error: NSDictionary?) -> Void

// Normal http request with JSON response..
func callJSONrequest(url:String, params:[String: AnyObject]?, success successBlock :apiSuccess,
                     failure failureBlock :apiFailure) {

    Alamofire.request(.POST, url, parameters: params, encoding: ParameterEncoding.URL)
        .responseJSON { response in
            print("\(response.request?.URL)")  // original URL request
            //print(response.response) // URL response
            //print(response.data)     // server data
            //print(response.result)   // result of response serialization
            if response.result.isSuccess {
                let jsonDic = response.result.value as! NSDictionary
                successBlock(result: jsonDic)

            } else {
                let httpError: NSError = response.result.error!
                let statusCode = httpError.code
                let error:NSDictionary = ["error" : httpError,"statusCode" : statusCode]
                failureBlock(error: error)
            }
    }
}

func myFunction() {
    let myApiSuccess: apiSuccess = {(result: NSDictionary?) -> Void in
        print ("Api Success : result is:\n \(result)")
        // Here you can make whatever you want with result dictionary
    }

    let myApiFailure: apiFailure = {(error: NSDictionary?) -> Void in
        print ("Api Failure : error is:\n \(error)")
        // Here you can check the errors with error dictionary looking for http error type or http status code
    }
    var params :[String: AnyObject]?
    let email : String! = "stuart@gmail.com"
    let password : String! = "thisismypassword"
    params = ["email" : email, "password" : password]
    let url : String! = "https://arcane-brook-75067.herokuapp.com/login"
    callJSONrequest(url, params: params, success: myApiSuccess, failure: myApiFailure)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM