Navya
Navya

Reputation: 39

How to send URL Request using Rxalamofire in swift4

Actually i am new to iOS, and now my task is sending URL Request with Rxalamofire.I am Completely not aware of Rxalamofire. Previously I was using only alamofire. Previously I was using only alamofire.Now also I have sent url request like previous using previous, but later I found that Rxalamofire is much better than alamofire.Unfortunatly I am not able to send the URL request.So,Can any one tell me the step by step process.Thanks in advance.

 postParameters = ["username":mailid,"password":password]

            Alamofire.request(Constants.loginapi, method: .post, parameters: postParameters, encoding: URLEncoding.default, headers: nil).responseJSON {  response in
                switch response.result {
                case .success:
                    print(response)
                  case .failure(let error):
                    print(error)
                }

            }

Upvotes: 1

Views: 1935

Answers (1)

Saurabh Yadav
Saurabh Yadav

Reputation: 957

I saw your earlier post struggling with MVVM/RxSwift.

You can find a project here that can help you with following-

  1. RXSwift + MVVM
  2. Rxswift + Alamofire

https://github.com/saurabh-360/RxAlamofireDemo

For the question, You can send the request the standard way,

But while using the RXswift API, it is better approach to use Observable pattern, which is observing streams of data.

Below code can help you get started.

func loginUser() -> Observable<Any>? {

    return Observable<Any>.create({observer in
        /**
         parameters or additional headers we can bind with the url request
         the case is standard here like we do in URLSession requests
         consider this example to incorporate the same
         https://stackoverflow.com/a/40608923/4549304
         */
        let parameters:[String:String] = ["username":"Your username value here",
                                          "password":"Your password value here"]

        /**
         There are multiple ways to request data from the servers using
         Default ALamofire methods
         I am using Alamofire.request(String) method here for the same.


         We Can also use
         request(_ urlRequest: URLRequestConvertible)
         method in case we want to make a post request
         with additional headers and parameters

         */

        Alamofire.request("https://reqres.in/api/login", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)
            .response { response in
                if response.response?.statusCode == 400 {
                    print("authorisation error")

                    // you need to exit the request here since there was error and
                    // no parsing is needed further for the response
                    // you can also send and error using the observer.OnError(Error)
                    return observer.onCompleted()
                }

                // convert data to our model and update the local variable
                guard let responseData =  response.data else {
                    return observer.onCompleted()
                }
                do {
                    let model = try JSONDecoder().decode(YourModel.self, from: responseData)
                    observer.onNext(model)
                    observer.onCompleted()
                }catch {
                    observer.onError(error)
                    observer.onCompleted()
                    print("some thing went wrong.")
                }
        }

        return Disposables.create();
    })
}

Upvotes: 2

Related Questions