Hi I got problem with makeing request to server, first I send request with login params and I get good response, after that I am trying to send another request to get info from server and I got error. For login I am using post method and it's work fine, but for second request with get method I get the error below. I found answer about error andI tried to change resposeJSON to responseString and I get other error, I think it's depend on bad token, but I don't know how to get token from responseString, could you help me with solution?
[Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
After changing to responseString I get:
SUCCESS
[Request]: GET http://192.168.1.1/ubus
[Response]: <NSHTTPURLResponse: 0x60800003b0c0> { URL: http://192.168.1.1/ubus } { status code: 400, headers {
Connection = "Keep-Alive";
"Content-Type" = "text/html";
"Keep-Alive" = "timeout=20";
"Transfer-Encoding" = Identity;
} }
[Data]: 35 bytes
[Result]: SUCCESS: Bad RequestInvalid Request
Request fuction:
private func makeWebServiceCall (urlAddress: String, requestMethod: HTTPMethod, params:[String:Any], completion: @escaping (_ JSON : Any) -> ()) {
Alamofire.request(urlAddress, method: requestMethod, parameters: params, encoding: JSONEncoding.default).responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result)
debugPrint(response)
switch response.result {
case .success(let value):
let json = JSON(value)
if let jsonData = response.result.value {
completion(jsonData)
}
case .failure(let error):
completion("Failure Response: \(error)")
}
Parameters class:
class JsonRequests {
static let loginToken = "00000000000000000000000000000000"
static func loginRequest(userName: String, password: String) -> [String:Any] {
let loginparam: [String : Any] = ["jsonrpc": "2.0",
"id": 1,
"method": "call",
"params": [ self.loginToken, "session", "login", [ "username": userName, "password": password]]
]
return loginparam
}
static func getInformationFromConfig(token: String, config: String, section : String, option: String) -> [String:Any] {
let getInformationFromConfigparam: [String : Any] = ["jsonrpc": "2.0",
"id": 1,
"method": "call",
"params": [ token, "uci", "get", [ "config": config, "section": section, "option": option]]
]
return getInformationFromConfigparam
}
}
fuction to call request:
public func login(userName: String, password: String, loginCompletion: @escaping (Any) -> ()) {
let loginrequest = JsonRequests.loginRequest(userName: userName, password: password)
makeWebServiceCall(urlAddress: URL, requestMethod: .post, params: loginrequest, completion: { (JSON : Any) in
loginCompletion(JSON)
})
}
public func device(token: String, loCompletion: @escaping (Any) -> ()) {
let deviceinfo = JsonRequests.getInformationFromConfig(token: token, config: "wireless", section: "@wifi-iface[0]", option: "mode")
makeWebServiceCall(urlAddress: URL, requestMethod: .get, params: deviceinfo, completion: { (JSON : Any) in
loCompletion(JSON)
})
}