CentreStack offers a RESTful API (Representational State Transfer Application Programming Interface) that you can use to automate most tasks via simple HTTP requests (Hypertext Transfer Protocol), such as posts, gets, deletes, etc.
You can use any programming or scripting language to perform HTTP requests and the server responses are usually JSON (Javascript Object Notation) or XML (Extensible Markup Language) objects.
The actual API documentation contains the full reference along with the mandatory "Hello World," but in this article I will show you a Powershell example using two back-to-back API calls with simple response parsing. If you understand this example, you will be able to use any of the documented API functions, because most of the requests require an authentication cookie to be obtained first and then passed in the HTTP Header of any subsequent calls. Most calls also require an XML object to be passed in the HTTP body, and the example in this article shows you how to do both of these tasks.
Here is an example that uploads a small zip called c:\TMP\gladr.zip to the root of the Home storage. It performs the following tasks in succession:
1-Logs in a user and obtains an authentication cookie for future requests
2-Uploads a file using the authentication cookie
3-Invokes the API method to generate a public link for the file
4-Outputs the public link to the screen
This script can be modified to ask the user for a file location and file name, but for the sake of simplicity, these values have been hard-coded. Please replace them with something else of your choosing. The proxiedupload.up endpoint has a 20MB file limit. Larger file sizes need to use multi-part uploading, which is not covered by the public API.
The code should be self-explanatory since many comments have been added on the lines starting with a pound sign (#). Please customize the login credentials and IP access point where indicated.
To implement the script, simply copy the entire code between the equal sign lines below into a notepad text file and name it "UPLOAD_FILE_AND_SHARE_API_POWERSHELL_EXAMPLE.ps1".
Here is the full API documentation.
#====================================================
#UPLOAD FILE AND SHARE - API POWERSHELL EXAMPLE
#Step 1: First we log in as the user and obtain an authentication cookie
#Here we prepare the XML object body for the authentication call. Change the values between the username and password XML fields below
$ReqBody1 = @"
<?xml version="1.0" encoding="UTF-8"?>
<JsonLogin xmlns="http://tempuri.org/">
<username>azeem@located6j.com</username>
<password>test</password>
</JsonLogin>
"@;
#The following code will obtain an authentication cookie and save it in the $cookie variable. Change the IP address below to your own IP or domain name
$response1 = Invoke-RestMethod "http://10.0.0.164/namespace/n.svc/jsonlogin" -Method Post -ContentType "application/xml" -Body $ReqBody1;
$cookie = $response1.JsonLoginResult.Cookie;
#Write-Host "Logged in... got cookie " $cookie;
#--------------------------------------------------------------
#Step 2: Upload a file
#We pass the cookie obtained above in the Header of the next call
$headers2 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers2.Add("x-glad-token", $cookie);
$headers2.Add("x-glad-altpath", "/gladr.zip");
$errorCode = "";
#PUT the file to proxiedupload.up
try {
Invoke-RestMethod -Uri "http://10.0.0.164/storage/proxiedupload.up" -Method Put -InFile "C:\TMP\gladr.zip" -Headers $headers2;
} catch {
$errorCode = $_.Exception.Response.StatusCode.value__;
Write-Host "Error:" $_.Exception.Response.StatusDescription;
}
#--------------------------------------------------------------
#Step 3:Create a public link for the file
if($errorCode -eq ""){
$headers3 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers3.Add("x-glad-token", $cookie);
$ReqBody3 = @"
<?xml version="1.0" encoding="UTF-8"?>
<EnableFilePublicLink xmlns="http://tempuri.org/">
<FileUri>/gladr.zip</FileUri>
</EnableFilePublicLink>
"@;
$response3 = Invoke-RestMethod -Uri "http://10.0.0.164/namespace/n.svc/enablefilepubliclink" -Method POST -ContentType "application/xml" -Body $ReqBody3 -Headers $headers3
if($response3.Success -eq "true"){
Write-Host "Uploaded File Successfully";
Write-Host "Public Link: " $response3.Context;
}
}else{
Write-Host "Upload failed!";
}
#====================================================
#====================================================
Comments
0 comments
Article is closed for comments.