【问题标题】:Send Alamofire request with parameters in swift快速发送带有参数的 Alamofire 请求
【发布时间】:2018-11-14 06:23:00
【问题描述】:

我想快速发送一个带有一些参数的请求。 body 也有 XML 内容。 http://example.com/POST

Body is : xml = "<root>abc</root>"
user : "admin"
ID : "21User"

我发现Alamofire 代码包含responseJSON,但我的响应不是 JSON。 它将是 XML。那么如何使用呢?

【问题讨论】:

    标签: ios swift alamofire


    【解决方案1】:

    Alamofire 请求可能如下所示:

    Alamofire.request(.POST, "https://something.com", parameters: Dictionary() , encoding: .Custom({
                (convertible, params) in
                let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
    
                let data = (self.testString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
                mutableRequest.HTTPBody = data
                mutableRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
                return (mutableRequest, nil)
            }))
        .responseJSON { response in
        print(response.response) 
        print(response.result)   
        }
    }
    

    尝试相同,但使用 responseData 或 responseString 而不是 responseJSON 。

    【讨论】:

    • 很抱歉,这是一个 POST 请求
    • 我收到一个错误,调用中有额外的参数。你是如何使用定义参数的?
    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2016-12-25
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    相关资源
    最近更新 更多