首页 > 解决方案 > 如何在帖子正文中使用 RxAlamofire 传递 json 进行帖子调用

问题描述

我想使用RxAlamofire进行 post 调用,它提供了函数 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)

标签: iosswiftswift4rx-swiftrxalamofire

解决方案


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

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

推荐阅读