0

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)
    })
}
Priyal
  • 879
  • 1
  • 9
  • 30
Egle Matutyte
  • 225
  • 1
  • 8
  • 18
  • A 400 means that the request was malformed. In other words, the data stream sent by the client to the server didn't follow the rules. You need to check with your backend developer what are they expecting. It can be a problem of Content-Type, missing authentication token in header, it can be problem of any norm coded by backend which request doesn't adhere to. – Priyal Feb 07 '17 at 12:48
  • For a better understanding check : http://stackoverflow.com/questions/19671317/400-bad-request-http-error-code-meaning – Priyal Feb 07 '17 at 12:50
  • I already checked this one, I am makeing request with terminal and it's working good, I am makeing this request and I get Json. curl -d "{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"d63b35e8d64e471503136f501b326c91\",\"uci\",\"get\",{\"config\":\"wireless\", \"section\":\"@wifi-iface[0]\", \"option\":\"mode\"}]}" http://192.168.1.1/ubus – Egle Matutyte Feb 07 '17 at 12:52
  • Could you help me if I am making request and get .responseString, How I can get token from response with SwiftyJson? – Egle Matutyte Feb 07 '17 at 12:57
  • I guess `.responseJSON` will be more useful. I haven't used SwiftyJson but can surely help you with that. – Priyal Feb 07 '17 at 13:00
  • But if I am using .responseJSON, then I am getting error: `[Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))` – Egle Matutyte Feb 07 '17 at 13:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135074/discussion-between-priyal-and-egle-matutyte). – Priyal Feb 07 '17 at 13:05
  • Hi, I have same problem, i have two wcf service with two different url but same config, Postman works ok with two url but Alamofire with one url is ok and other return error 400 – M.R Jul 09 '18 at 06:21

0 Answers0