How to download a file from Parse.com using the REST API?

I found this question when asked about downloading the Parse.com file , however it only mentions that the file can be downloaded from Url. In addition, Parse.com REST Documentation discusses only the download file and is associated with the object.

I tried to access only the url but it returns an error.

Can someone help with an example in Swift and using the REST API how to load the actual file after receiving the url after requesting the object?

This is the error I get:

Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x7fa39940dcc0 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 "(null)"}

This is my code in Swift 2.0:

func downloadFile(){
    let str = "http://files.parsetfss.com/c426b506-44da-447d-91d0-93f13980758b/tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg"
    let request = NSMutableURLRequest()
    request.HTTPMethod = "GET"
    request.addValue(appID, forHTTPHeaderField:  "X-Parse-Application-Id")
    request.addValue(apiKey, forHTTPHeaderField: "X-Parse-REST-API-Key")

    let url = NSURL(fileURLWithPath: str)
    request.URL = url

    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request, completionHandler: {
        (data, response, error) in
        if (error == nil) {
            do {
                let image = try NSJSONSerialization.dataWithJSONObject(data!, options: [])
            } catch {

            }
        }
    })
    task.resume()
}

This is the JSON response that I get when I request the object, and the URL is what I need to use to get the file:

"picture": {
"__type" = File;
name = "tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg";
url = "http://files.parsetfss.com/c426b506-44da-447d-91d0-93f13980758b/tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg";
+4
1

, :

let url = NSURL(fileURLWithPath: str)

"str" - URL-, , , API , URL- "file:///" , .

:

let url = NSURL(string: str)

, ?

+1

Source: https://habr.com/ru/post/1620937/


All Articles