状态代码:400 swift 3 alamofire

时间:2017-02-07 12:37:44

标签: json swift xcode alamofire

嗨我遇到了向服务器发出请求的问题,首先我发送带有登录参数的请求,我得到了很好的响应,之后我试图发送另一个请求从服务器获取信息,我得到了错误。对于登录我使用post方法,它工作正常,但对于使用get方法的第二个请求,我得到下面的错误。我找到了关于错误的答案,我试图将resposeJSON更改为responseString并且我得到其他错误,我认为这取决于坏令牌,但我不知道如何从responseString获取令牌,你能帮我解决一下吗?

[Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

更改为responseString后,我得到:

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

请求功能:

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)")
}

参数类:

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
}
}

致电请求:

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)
    })
}

0 个答案:

没有答案