TRON网址迅速发布

时间:2017-03-01 17:15:11

标签: ios iphone json swift

我正在使用TRON框架进行swift,我因为我的网址而收到错误。 这是代码:

let relatedTronUrl = TRON(baseURL: "https://api.themoviedb.org")

func fetchRelatedFeed(id: Int, completion: @escaping () -> ()){
    let request: APIRequest<Related, JSONError> = relatedTronUrl.request("/3/tv/1402/similar?api_key=myAPIkey&language=en-US&page=1")

    request.perform(withSuccess: { (related) in
        print("Successfully fatched our json objects")
        completion()
    }) { (err) in
        print("Failed to fetch json ",err)
    }
}

正如您所看到的,正确的网址应该是:https://api.themoviedb.org/3/tv/1402/similar?api_key=myAPIkey&language=en-US&page=1 但是当我运行我的应用程序时,我收到一个JSON错误:

Failed to fetch json  APIError<JSONError>(request: Optional(https://api.themoviedb.org/3/tv/1402/similar%3Fapi_key=myAPIkey&language=en-US&page=1), response: Optional(<NSHTTPURLResponse: 0x610000235f20> { URL: https://api.themoviedb.org/3/tv/1402/similar%3Fapi_key=myAPIkey&language=en-US&page=1 } { status code: 401, headers {
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Length" = 86;
"Content-Type" = "application/json;charset=utf-8";
Date = "Wed, 01 Mar 2017 16:58:38 GMT";
Server = openresty;
Status = "401 Unauthorized";
"X-RateLimit-Limit" = 40;
"X-RateLimit-Remaining" = 39;
"X-RateLimit-Reset" = 1488387528;} }), data: Optional(86 bytes), error: Optional(Alamofire.AFError.responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(401))), errorModel: Optional(BookFinde.RelatedService.JSONError))

我认为问题是由于TRON的请求,因为正确的网址是:https://api.themoviedb.org/3/tv/1402/similar?api_key=myAPIkey&language=en-US&page=1
但我得到的是:https://api.themoviedb.org/3/tv/1402/similar%3Fapi_key=myAPIkey&language=en-US&page=1
正如您所看到的,差异是?转换为%3F。 我做错了什么?

1 个答案:

答案 0 :(得分:4)

这是我找到的解决方案:

let relatedTronUrl = TRON(baseURL: "https://api.themoviedb.org")

func fetchRelatedFeed(id: Int, completion: @escaping () -> ()){
    let request: APIRequest<Related, JSONError> = relatedTronUrl.request("3/tv/\(id)/similar")

    // I had to manually add the paramaters in this way
    request.parameters = ["api_key":"myAPIkey","language":"en-US","page":"1"]

    request.perform(withSuccess: { (related) in
        print("Successfully fatched our json objects")
        completion()
    }) { (err) in
        print("Failed to fetch json ",err)
    }
}