From f00d41394344a219b7b4140db2a76b908b2fa2f8 Mon Sep 17 00:00:00 2001 From: Raju Udava <86527202+dstala@users.noreply.github.com> Date: Tue, 18 Jul 2023 20:35:40 +0530 Subject: [PATCH] chore: upload api documentation Signed-off-by: Raju Udava <86527202+dstala@users.noreply.github.com> --- .../en/developer-resources/upload-via-api | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 packages/noco-docs/content/en/developer-resources/upload-via-api diff --git a/packages/noco-docs/content/en/developer-resources/upload-via-api b/packages/noco-docs/content/en/developer-resources/upload-via-api new file mode 100644 index 0000000000..95ea69e904 --- /dev/null +++ b/packages/noco-docs/content/en/developer-resources/upload-via-api @@ -0,0 +1,140 @@ +--- +title: 'Upload via API' +description: 'Upload files locally present or from public remote URL via API' +position: 1600 +category: 'Developer Resources' +menuTitle: 'Upload via API' +--- + +Sample code to upload files via API is listed below. +Assumes `http://localhost:8080/` as the base URL for the API calls. + +# Upload local file + +``` +let axios = require("axios").default; +let FormData = require('form-data'); +let fs = require('fs'); + +// Configurations +// +const project_id = ''; +const table_id = ''; +const xc_token = ''; +const file_path = ''; + + +// Insert Image +// @param image_path : local file path +// @return : JSON object to be used in insert record API for attachment field +// +async function insertImage (path) { + const formData = new FormData(); + formData.append("file", fs.createReadStream(path)); + const data = await axios({ + url: 'http://localhost:8080/api/v1/db/storage/upload', + data: formData, + headers:{ + 'Content-Type':`multipart/form-data;`, + 'xc-auth': xc_token + }, + method: 'post', + + // Optional : storage file path + params: {"path": "somePath"} + }); + return data; +} + +// Insert record with attachment +// Assumes a table with two columns : +// 'Title' of type SingleLineText and +// 'Attachment' of type Attachment +// +async function uploadFileExample() { + let response = await insertImage(file_path); + + let row = { + "Title": "2", + "Attachment": response.data + }; + + await axios({ + method: 'POST', + url: `http://localhost:8080/api/v1/db/data/noco/${project_id}/${table_id}`, + data: row, + headers: { + 'xc-auth': xc_token + } + }); +} + +(async () => { + await uploadFileExample(); +})(); +``` + + +# Upload via URL + +``` +let axios = require("axios").default; +let FormData = require('form-data'); +let fs = require('fs'); + +// Configurations +// +const project_id = ''; +const table_id = '
'; +const xc_token = ''; + +// URL array : URLs of files to be uploaded +const URLs = [{ url: '' }, { url: '' }]; + +// Insert Image +// @param URLs : [] containing public URL for files to be uploaded +// @return : JSON object to be used in insert record API for attachment field +// +async function insertImageByURL (URL_array) { + const data = await axios({ + url: 'http://localhost:8080/api/v1/db/storage/upload-by-url', + data: URL_array, + headers: { + 'xc-auth': xc_token + }, + method: 'post', + + // Optional : storage file path + params: {"path": "somePath"} + }); + return data; +} + +// Insert record with attachment +// Assumes a table with two columns : +// 'Title' of type SingleLineText and +// 'Attachment' of type Attachment +// +async function uploadByUrlExample() { + let response = await insertImageByURL(URLs); + + // Update two columns : Title and Attachment + let row = { + "Title": "3", + "Attachment": response.data + }; + + await axios({ + method: 'POST', + url: `http://localhost:8080/api/v1/db/data/noco/${project_id}/${table_id}`, + data: row, + headers: { + 'xc-auth': xc_auth + } + }); +} + +(async () => { + await uploadByUrlExample(); +})(); +``` \ No newline at end of file