haedaldal haedaldal - 1 month ago 7
iOS Question

curl -L to get file contents from Box through Alamofire for Swift 3

I am trying to get the file contents on Box through their API in Swift.

curl -L https://api.box.com/2.0/files/file_id/content -H "Authorization: Bearer access_token"


returns the right contents, but

curl https://api.box.com/2.0/files/file_id/content -H "Authorization: Bearer access_token"


does not.
So the "-L" part seems to be critical.

So far I have

let headers = [
"grant_type": "client_credentials",
"Authorization": "Bearer \(token)",
"scope": "public"
]

Alamofire.request("https://api.box.com/2.0/files/file_id/content", headers: headers).responseJSON { responseFile in

if let dataFile = responseFile.result.value {
print("JSON: \(dataFile)")
}
}


How can I add the "-L" part to this?

The overall structure should be correct since I can successfully get the metadata for the file by removing "/content" from the url.

Answer

According to curl manual -L/--location argument means

If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.

So, I guess, you must check response statusCode and if it will be 3xx you need to handle redirects. Check Alamofire framework documentation about that.

Also good to read about fundamentals Handling Redirects and Other Request Changes

Comments