ios 发布应用程序/x-www-form-urlencoded Alamofire

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37177879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:03:37  来源:igfitidea点击:

post application/x-www-form-urlencoded Alamofire

iosswift2alamofire

提问by Mario Dennis

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?

我想使用 Alamofire 从 Web API 检索不记名令牌,但我是 ios 和 alamofire 的新手。我怎样才能用 Alamofire 做到这一点?

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

回答by Majster

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

那么你真的不需要 Alamofire 来做到这一点(它可以简单地使用一个 plain 来完成NSURLRequest)但这里是:

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)
}

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

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

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

您还可以在此处找到大量带有示例的规范。

回答by Max Desiatov

Here is example code that should work with Alamofire 4.x and Swift 3.x as of August 2017:

以下是截至 2017 年 8 月适用于 Alamofire 4.x 和 Swift 3.x 的示例代码:

let parameters = [
  "myParameter": "value"
]
Alamofire.request("https://something.com", method: .post, parameters: parameters, encoding: URLEncoding()).response { response in
  print(response.request)
  print(response.response)
  print(response.data)
  print(response.error)
}

There is no need to set the content-type header explicitly, as it is set by Alamofire automatically.

不需要显式设置 content-type 标头,因为它是由 Alamofire 自动设置的。

回答by Suhit Patil

Alamofire 4.7.3 and Swift 4.0 above

Alamofire 4.7.3 和 Swift 4.0 以上

As per the documentationfor POST Request With URL-Encoded Parameters

根据带有 URL 编码参数的 POST 请求的文档

let parameters: Parameters = [
    "foo": "bar", 
    "val": 1 
]

// All three of these calls are equivalent
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: URLEncoding.default)
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: URLEncoding.httpBody)

// HTTP body: foo=bar&val=1