发布应用程序/ x-www-form-urlencoded Alamofire [英] post application/x-www-form-urlencoded Alamofire

查看:176
本文介绍了发布应用程序/ x-www-form-urlencoded Alamofire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Alamofire从Web API检索承载令牌,但是我对ios和alamofire还是陌生的。如何使用Alamofire完成此操作?

I want to use Alamofire to retrieve a bearer token from Web API but I am new to ios and alamofire. How can I accomplish this with Alamofire?

func executeURLEncodedRequest(url: URL, model: [String : String]?, handler: RequestHandlerProtocol) {
    addAuthorizationHeader()
    Alamofire.request(.POST,createUrl(url), parameters: model, headers: headers,encoding:.Json)
}


推荐答案

好吧,您真的不需要Alamofire来做到这一点(可以简单地完成使用普通的 NSURLRequest ),但操作如下:

Well you don't really need Alamofire to do this (it can be simply done using a plain NSURLRequest) but here goes:

let headers = [
    "Content-Type": "application/x-www-form-urlencoded"
]
let parameters = [
    "myParameter": "value"
]
let url = NSURL(string: "https://something.com")!
Alamofire.request(.POST, url, parameters: parameters, headers: headers, encoding: .URLEncodedInURL).response { request, response, data, error in
    print(request)
    print(response)
    print(data)
    print(error)
}

我认为标题可以省略,因为alamofire将附加适当的 Content-Type 标题。让我知道它是否有效。

I think that the headers can be omitted since alamofire will append the appropriate Content-Type header. Let me know if it works.

您还可以找到大量的规范此处提供示例

You can also find a ton of specification with examples here.

这篇关于发布应用程序/ x-www-form-urlencoded Alamofire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆