How to make Post call using RxAlamofire pass json in post body

415 views Asked by At

I want to make a post call using RxAlamofire it provides function requestJson which requires two parameters type get or post and url but there is no parameter to pass post json body how to do that following is my code

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

There are 1 answers

0
boa_in_samoa On

Actually the other parameters are included in the definition of requestJson, they just have default parameters. So you can safely say:

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