0

Hi I new at swift and I got problem with server connection. I am trying to make request using a method post to login to server using username and password. How I can connect to the server and authentificate?

error messsage:

 ["id": <null>, "error": {
    code = "-32700";
    message = "Parse error";
}, "jsonrpc": 2.0]

Code:

class ViewController: UIViewController {

override func viewDidLoad() {
   var userName = "root"
   var password = "admin01"

 //   http://192.168.1.1/ubus
   //var LOGIN_TOKEN = 0000000000000000
    let url: String = "http://192.168.1.1/ubus"
    let param: [String: Any] = ["username": "admin", "password": "admin01", "token": "0000000000000000"]
    Alamofire.request(url, method: .post, parameters: param,
                      encoding: JSONEncoding.default)
        .responseJSON { response in
            guard response.result.error == nil else {
                // got an error in getting the data, need to handle it
                print("error calling post")
                print(response.result.error!)
                return
            }

            // make sure we got some JSON since that's what we expect
            guard let json = response.result.value as? [String: Any] else {
                print("didn't get todo object as JSON from API")
                print("Error: \(response.result.error)")
                return
            }

            print(json)
    }


    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Json at server which I need to get:

{"jsonrpc":"2.0","id":1,"result":[0,{"ubus_rpc_session":"07e111d317f7c701dc4dfde1b0d4862d","timeout":300,"expires":300,"acls":{"access-group":{"superuser":["read","write"],"unauthenticated":["read"]},"ubus":{"*":["*"],"session":["access","login"]},"uci":{"*":["read","write"]}},"data":{"username":"root"}}]}

Request should look like:

"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"admin01\"  } ] }"
ThunderStruct
  • 1,504
  • 6
  • 23
  • 32
Gediminas Urbonas
  • 233
  • 1
  • 2
  • 10

1 Answers1

0

Try to use this code:

 Alamofire.request(url, method: .post, parameters: parameters).responseJSON { response in
                    if(response.result.isFailure){
                        print("no data!");

                    }else{
                      print("received data!");

// make sure we got some JSON since that's what we expect
            guard let json = response.result.value as? [String: Any] else {
                print("didn't get todo object as JSON from API")
                print("Error: \(response.result.error)")
                return
            }

            print(json)
                    }
                }
Vishal Sonawane
  • 2,637
  • 2
  • 16
  • 21