1

I am new at Swift 3 and I got problems with getting Json return then sending request. I am trying to send a post request to the server with parameters username and password and get a response with Json with information, but I haven't been able to get the data to return.

Output:

error: NSURLErrorDomain: -1003 status code: 200, headers Connection = close; "Content-Type" = "application/json"; "Transfer-Encoding" = Identity

request on Android looks like:

 "{\n" + " \"jsonrpc\": \"2.0\",\n" + " \"id\": \"1\",\n" + " \"method\": \"call\",\n" + " \"params\": [\n" + " \"" + LOGIN_TOKEN + "\",\n" + " \"session\",\n" + " \"login\",\n" + " {\n" + " \"username\": \"" + userName + "\",\n" + " \"password\": \"" + password + "\"\n" + " }\n" + " ]\n" + "}"; 

this is what I should send to request:

"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"admin01\" } ] }" 

This is my code:

 override func viewDidLoad() { var userName = "root" var password = "admin01" //var LOGIN_TOKEN = 0000000000000000 let jsonObject: [String: Any] = ["jsonrpc" : 2.0, "id": 1, "method": "call", "params": [ "00000000000000", "session", "login", [ "username": userName, "password": password]], ] do { let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted) // here "jsonData" is the dictionary encoded in JSON data let decoded = try JSONSerialization.jsonObject(with: jsonData, options: []) // here "decoded" is of type `Any`, decoded from JSON data // you can now cast it with the right type if let dictFromJSON = decoded as? [String:String] { // use dictFromJSON } } catch { print(error.localizedDescription) } Alamofire.request("http://192.168.1.1/ubus", method: .post, parameters: jsonObject, encoding: JSONEncoding.default) .responseJSON { response in print(response) //to get status code if let status = response.response?.statusCode { switch(status){ case 201: print("example success") default: print("error with response status: \(status)") } } //to get JSON return value if let result = response.result.value { let JSON = result as! NSDictionary 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. } 
1

1 Answer 1

1

Your JSON object seems to be invalid

There are really helpful JSON validators that are free. One that comes to mind is http://jsonlint.com

I think, from what I can interpret by the code you posted, that you want your jsonObject to look something like this, which is a valid JSON:

[ { "jsonrpc": 2.0, "id": 1, "method": "call", "params": ["00000000000000", "session", "login"], "username": username, "password": password } ] 

I assume the username and password variables are strings you have already assigned?

Sign up to request clarification or add additional context in comments.

2 Comments

I got some errors. After "jsonrpc" need ; and any type cant be used with arrays, I got request onAndroid studio and its working perfectly.
@GediminasUrbonas - That's because it's JSON code, and you're trying to make it a Swift object. You should look into creating a JSON file with the above code, and then learn how to convert JSON to Data. You can't just cast JSON code as Swift Data, it has to be converted somehow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.