0

I want to make post call using RxAlamofire not able to find any method to do so

tried using requestJSON method but there is no paramter to pass post json in

 RxAlamofire.requestJSON(.post, url)

how to make post call and pass json data to post call in RxAlamofire

alphanso
  • 409
  • 5
  • 22
  • Look into [this](https://github.com/RxSwiftCommunity/RxAlamofire/blob/master/Sources/RxAlamofire.swift) to find all the methods. – Kamran May 21 '19 at 06:48
  • unable to find how to pass post json. Which parameter as dictionary or json string not clear – alphanso May 21 '19 at 06:53
  • you may want to put the JSON string in parameters? like request(...,parameters: ["json_str": yourJsonString],...) – Ming Chu May 25 '19 at 15:04

4 Answers4

1

Use following code

  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()
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
0

use this function with proper parameters ancoding

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

amodkanthe's solution works. But its return value is not friendly. So I changed it a litte bit

    var request = URLRequest(url: URL(string: "https://some_url")!)
    request.httpBody = jsonData  // jsonData is a Data type
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type") // this line is important
    RxAlamofire.requestJSON() //=> returns a Observable<(HttpURLResponse, Any)>, the `Any` type is actually a dictionary

p.s. the version I'm using is RxAlamofire(6.1.1)

Songzhw
  • 131
  • 1
  • 3
-1

Using jsonEncoding

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

It works for me~

Versus
  • 404
  • 4
  • 13