获取请求返回错误代码405 - 不允许使用方法

时间:2017-03-09 11:03:47

标签: swift swift3 nsurlsession

我已经为其余的GET请求编写了一个函数,它说我的方法不允许 - 代码405是werid而我无法找到解决方案。

我正在通过登录后分配给用户的当前令牌进行GET。

有人可以查看代码并告诉我可能出错的地方吗?

func getRequest() -> Void {

    let json: [String: Any] = ["token": SessionMenager.Instance.token]

    let jsonData = try? JSONSerialization.data(withJSONObject: json)

    // create post request
    let url = URL(string: MY_URL)!
    var request = URLRequest(url: url)
    request.httpMethod = "GET"

    // insert json data to the request
    request.httpBody = jsonData
    request.setValue("application/json;charest=utf-8", forHTTPHeaderField: "Content-Type")

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            print(error?.localizedDescription ?? "No data")
            return
        }

        if let httpResponse = response as? HTTPURLResponse {
            print("GET : code -  \(httpResponse.statusCode)")
        }

        let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
        if let responseJSON = responseJSON as? [String: Any] {
            print(responseJSON)
        } else{
            print(error.debugDescription)
        }
    }

    task.resume()
}

提前致谢!!

0 个答案:

没有答案