【问题标题】:How to make Post call using RxAlamofire pass json in post body如何在帖子正文中使用 RxAlamofire 传递 json 进行帖子调用
【发布时间】:2019-05-20 19:55:31
【问题描述】:

我想使用 RxAlamofire 进行发布调用,它提供 function requestJson 需要两个参数 输入get或post和url,但没有没有参数可以传递post json body如何做到以下是我的代码

 var components = URLComponents(string: url)!
    components.setQueryItems(with: param)
    let url = components.url!
    print("\(url)")

    RxAlamofire.requestJSON(.post, url)
        .subscribe(onNext: { [weak self] (r, json) in
            if let jsonResult = JSON(json) as? JSON {
                if let cartResult = FoodCartResult(jsonResult) as? FoodCartResult {
                    self?.delegate?.showCart(cartresult: cartResult)
                }
            }

            }, onError: {  [weak self] (error) in
                print(error.localizedDescription)
                self?.delegate?.onError()
            },onCompleted: {})
        .disposed(by: disposeBag)

【问题讨论】:

    标签: ios swift swift4 rx-swift rxalamofire


    【解决方案1】:

    其实其他参数都包含在requestJson的定义中,只是有默认参数而已。所以你可以放心地说:

    RxAlamofire.requestJSON(.post,
                            url,
                            parameters: ["param1": "value1"],
                            encoding: JSONEncoding.default,
                            headers: nil)
    

    【讨论】:

      猜你喜欢
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多