【问题标题】:RxAlamofire make post call with json bodyRxAlamofire 使用 json 正文进行 post call
【发布时间】:2019-05-21 06:43:24
【问题描述】:

我想使用 RxAlamofire 进行后期调用,但找不到任何方法

尝试使用 requestJSON 方法,但没有参数来传递 post json

 RxAlamofire.requestJSON(.post, url)

如何在 RxAlamofire 中进行 post call 并将 json 数据传递给 post call

【问题讨论】:

  • 查看this 以找到所有方法。
  • 无法找到如何传递 post json。哪个参数作为字典或json字符串不清楚
  • 您可能希望将 JSON 字符串放入参数中?比如 request(...,parameters: ["json_str": yourJsonString],...)

标签: swift swift4 rx-swift rxalamofire


【解决方案1】:

使用以下代码

  var request = URLRequest(url: URL(string: "https://some_url")!)
    //Following code to pass post json 
    request.httpBody = json
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    RxAlamofire.request(request as URLRequestConvertible).responseJSON().asObservable()

【讨论】:

    【解决方案2】:

    将此函数与适当的参数编码一起使用

    public func urlRequest(_ method: Alamofire.HTTPMethod,
                       _ url: URLConvertible,
                       parameters: [String: Any]? = nil,
                       encoding: ParameterEncoding = URLEncoding.default,
                       headers: [String: String]? = nil)
    

    【讨论】:

    【解决方案3】:

    使用 jsonEncoding

    RxAlamofire.requestJSON(.post, url, encode: JsonEncoding.default)

    对我有用~

    【讨论】:

      猜你喜欢
      • 2020-09-21
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2012-04-12
      相关资源
      最近更新 更多