使用alamofire的https请求失败

托默·阿巴迪

我对快速编程非常陌生,所以这可能是愚蠢的错误。无论如何,当我执行要求HTTPS_REQUIRED althoge的网址请求时,即使我在浏览器中手动检查了网址字符串,我的代码也会失败

我尝试在plist文件中添加“应用程序传输安全设置”键,但它也不起作用。

func getImage(url : String){

    print(url)
    Alamofire.request(url, method: .get).responseJSON {
        response in
        if response.result.isSuccess {
            print(response.result.value)
        }
        else {
            print("Error \(response.result.error)")
            self.loadingLable.text = "Network Error"
        }
    }

}

//MARK:- Build the url request
func constructUrlRequest(latitude : String, longitude : String) {
    urlComponets.scheme = "https"
    urlComponets.host = "api.nasa.gov"
    urlComponets.path = "/planetary/earth/imagery"
    urlComponets.queryItems = [
        URLQueryItem(name : "lon", value : longitude),
        URLQueryItem(name : "lat", value : latitude),
        URLQueryItem(name: "api_key", value: "DEMO_KEY")
    ]
    let urlString = urlComponets.url?.absoluteString
    getImage(url: urlString!)

}

这是我的控制台输出:

longitude = -122.03051210999995, latitude = 37.33240904999999
https://api.nasa.gov/planetary/earth/imagery?lon=-122.03051210999995&lat=37.33240904999999&api_key=DEMO_KEY
Optional({
    error =     {
        code = "HTTPS_REQUIRED";
        message = "Requests must be made over HTTPS. Try accessing the API at: https://api.nasa.gov/planetary/earth/imagery/?lon=-122.03051210999995&lat=37.33240904999999&api_key=DEMO_KEY";
    };
})
Alxlives

您的代码是正确的,并且请求是在https中完成的,但是NASA API有点混乱。

刚刚查看了API文档,它重定向到/planetary/earth/imagery/,并且在请求上还需要其他变量。

尝试这个:

   urlComponets.scheme = "https"
   urlComponets.host = "api.nasa.gov"
   urlComponets.path = "/planetary/earth/imagery/"
   urlComponets.queryItems = [
       URLQueryItem(name : "lon", value : "100.75"),
       URLQueryItem(name : "lat", value : "1.5"),
       URLQueryItem(name: "date", value: "2014-02-01"),
       URLQueryItem(name: "cloud_score", value: "True"),
       URLQueryItem(name: "api_key", value: "DEMO_KEY")
   ]

输出:

Optional({
    "cloud_score" = "0.03926652301686606";
    date = "2014-02-04T03:30:01";
    id = "LC8_L1T_TOA/LC81270592014035LGN00";
    resource =     {
        dataset = "LC8_L1T_TOA";
        planet = earth;
    };
    "service_version" = v1;
    url = "https://earthengine.googleapis.com/api/thumb?thumbid=bc77b079c8ecd07cd668c576c22b83a4&token=51a4298b6aae4529caee9ed0b64e636f";
}) 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章