使用带有 POST 的 manager.request [英] Using manager.request with POST

查看:30
本文介绍了使用带有 POST 的 manager.request的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Alamofire SessionManager 发送一个 POST 请求.

I want to send a POST request with Alamofire SessionManager.

我阅读了有关的文档https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol

但我没有看到使用请求和POST的例子,只有上传.

but I don't see an example for using request and POST, only upload.

它给出的例子:

let parameters: Parameters = ["foo": "bar"]

Alamofire.request(urlString, parameters: parameters) // Encoding => URLEncoding(destination: .methodDependent)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding(destination: .queryString))
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding(destination: .httpBody))

// Static convenience properties (we'd like to encourage everyone to use this more concise form)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.default)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.queryString)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.httpBody)

我的代码是:

<代码>manager.request(url,方法:.post,参数:参数,编码:.url).responseJSON { 响应输入完成(响应)}

符合方法签名(据我所知)但我收到错误额外参数 method: in call.

which conforms to the method signature (from what I can tell) but I get an error "Extra parameter method: in call.

推荐答案

您需要将 URLRequest 与正文中的参数一致为 Data

You need conform URLRequest with your parameters in body as Data

此代码可以帮助您

    var request = URLRequest(url: urlString!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)

    manager.request(request)
           .responseJSON { response in
                fulfill(response)
        }

希望能帮到你

这篇关于使用带有 POST 的 manager.request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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