BaaS tips & tricks: Uploading files to Kinvey using HTTPS
With RAD Studio XE8, we support a range of backend as a service (BaaS) providers including Kinvey, Parse and App42.
Over the last year I have written many blog posts on our BaaS support and frequently get questions on BaaS from our customers. I recently got a question about how to upload files to Kinvey using https instead of http. Kinvey uses Google cloud storage for files and you can use either 'http:' or 'https:' when making a request to Google.
Below is some information from Kinvey's website:
"By default, the upload and download URLs generated by Kinvey use the
http protocol to communicate with Google Cloud Storage. However, you may optionally request an
https URL using the
tls query parameter."
To use https instead of http when making a request to Google, you will need to modify the URLs we get from Kinvey to have an 'https:' prefix. This can be done by editing the REST.Backend.KinveyAPI source file found in source/data/rest/. I would recommend saving a copy of the file in the same folder as your project. Then add the file to your project in Delphi XE8, and select to use the unit.
Here are the changes you will need to make to the REST.Backend.KinveyAPI source file:
1. Add this routine somewhere in the source code to change the prefix:
function ForceHTTPS(const AURL: string): string; begin if AURL.StartsWith('http:') then Result := 'https:' + AURL.Substring(5) else Result := AURL; end;
2. Modify the URL for upload (in TKinveyApi.RetrieveFile):
LJSONValue := LResponseObject.GetValue('_uploadURL'); // Do not localize if LJSONValue <> nil then LUploadURL := LJSONValue.Value; Assert(LUploadURL <> ''); //Change to HTTPS LUploadURL := ForceHTTPS(LUploadURL);
3. Modify the URL for download (TKinveyAPI.RetrieveFile):
LResponse := FRequest.Response.JSONValue as TJSONObject; AFile := FileFromObject(LResponse); //Change to HTTPS AFile.FDownloadURL := ForceHTTPS(AFile.FDownloadURL); if Assigned(AJSON) then AJSON.AddElement(LResponse.Clone as TJSONObject); if Assigned(AProc) then AProc(AFile, LResponse); end
For instructions on how to create an image upload/download app using BaaS, please have a look at this blog post.
Please login first in order for you to submit comments
- Page :