==== payloads:retrieve_payload ====
The payloads web service is a REST structured service that derives its functionality based on syntax used when calling the web service. To retrieve a payload from a repository, call the payloads web service with a repository name and the payload file name.
The payload will be returned in the body of the response as a UTF-8 encoded XML string.
**Important:** The payload must be locked prior to retrieving it.
----
**Purpose:** **Retrieve A Payload From A Repository.**
Request Endpoint
{{tablelayout?colwidth="75px,575px"&rowsFixed=1&rowsVisible=10&float=center}}
^ Method ^ URL ^
| GET | https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/payloads/{repository}/{payload_name} |
**Variables:**
{{tablelayout?colwidth="175px,75px,400px"&rowsFixed=1&rowsVisible=10&float=center}}
^ Variable ^ Usage ^ Description ^
| {repository} | Required | The name of the repository |
| {payload_name} | Required | The name of the payload |
Request Body
N/A
Request Headers
{{tablelayout?colwidth="150px,450px"&rowsFixed=1&rowsVisible=10&float=center}}
^ Key ^ Value ^
| access_token | {soft-token} |
JSON Request Parameters
N/A
**Notes:**
Any condition that results in an unsuccessful call to the payloads web service will produce a JSON formatted response with a code and message property populated.
Sample Request
**Endpoint:**
https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/payloads/retrieve/standard_testagent1-202007140415-00003.1.1_200723151458170.xml
Sample Response (Note: Most of the Base64 image text was removed for readability.)
Status Code: 200
<_ASSOCIATED_DOCUMENT _InstrumentNumber="2009003899200" _RecordingDate="2009-02-25" />
SUkqAA7qAAAtVZDKI+R4jhkhyPkfI6I6I4hfI4EGAXI4y4UuyODkcUjg5HA+LkR4uKXyOyOFI4YORcyPl2YiOBgjxHZeL5HIuyPnGR4ui6I6I
**OR**
Status Code: 500 (When an error occurs.)
{
"Exception": {
"Code": "PayloadFileLockError",
"Message": "The payload standard_testagent1-202007140415-00003.1.1_200723151458170.xml not locked."
}
}
----
**Comment 1:**
Following is a C# / .NET sample code snippet that shows how to call the web services associated with retrieve a payload from a remote folder/repository and saving it in a local folder.
This snippet was derived from the [[guides:web_service_toolbox_project|Web Service Toolbox]] that is available on the CeRTNA Wiki. The sample application is intended for easy to read demonstration purposes and not production quality code. The [[guides:web_service_toolbox_project|Web Service Toolbox]] application is documented, on the CeRTNA Wiki, to aid in understanding the code snippet shown below.
The general process flow for retrieving a payload from a remote folder is:
* Lock the remote payload.
* Retrieve the remote payload.
* Validate your retrieved payload.
* If the validation is successful: Delete the locked remote payload.
* Otherwise: Unlock the remote payload. (Troubleshoot or re-retrieve.)
The following snippets show the process, minus the validation, step...
** Snippet 1: **
private void btnRetrieve_Click(object sender, RoutedEventArgs e)
{
bool rc;
string strLocalFolder = tbLocalFolderName.Text;
string strRepository = (cbRepository.SelectedItem as ComboBoxItem).Content as string;
// See payloads:get_repository_file_names web servicefor the following
string[] strFolderFileNames = getRepositoryFileNames(strRepository);
if (strFolderFileNames != null)
{
for (int i = 0; i < strFolderFileNames.Length; i++)
{
// Lock the payload
rc = LockUnlockRemoteFile(strRepository, strFolderFileNames[i], "lock");
// Retrieve the payload
DownloadRemoteFile(strRepository, strFolderFileNames[i], strLocalFolder);
// Delete the remote payload
rc = DeleteRemoteFile(strRepository, strFolderFileNames[i]);
}
}
}
** Snippet 2: Lock The Payload **
private bool LockUnlockRemoteFile(string strRepositoryType, string strFileName, string strAction)
{
bool rc = false; //Default
if (strAction == "lock")
{
tbMessageLine.Text = "Locking remote file " + strFileName;
}
else if (strAction == "unlock")
{
tbMessageLine.Text = "Unlocking remote file " + strFileName;
}
else
{
tbMessageLine.Text = "";
}
RestClient client = null;
RestRequest request = null;
IRestResponse response = null;
string strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/user/login";
// Password has to be hashed.
// New convention uses a Salt value from the user record
// See the Login web service for more detail.
string strSalt = GetSalt(tbUserName.Text);
string strPassword = tbPassword.Password.ToString();
string strHashedPassword = HashPassword(strPassword, strSalt);
string strParms = "{\n\t\"user_name\" : \"";
strParms += tbUserName.Text;
strParms += "\",\n\t\"password\" : \"";
strParms += strHashedPassword;
strParms += "\"\n}";
client = new RestClient(strURL);
request = new RestRequest(Method.POST);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("Login", strParms, ParameterType.RequestBody);
response = client.Execute(request);
if (response.IsSuccessful)
{
var jsonResult1 = JsonConvert.DeserializeObject(response.Content);
string token = jsonResult1.access_token;
strURL = null;
client = null;
request = null;
response = null;
strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/payloads/" + strRepositoryType + "/" + strFileName + "?operation=" + strAction;
client = new RestClient(strURL);
request = new RestRequest(Method.PATCH);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("access_token", token);
response = client.Execute(request);
if (response.IsSuccessful)
{
if (strAction == "lock")
{
tbMessageLine.Text = "Remote file " + strFileName + " successfully locked.";
}
else if (strAction == "unlock")
{
tbMessageLine.Text = "Remote file " + strFileName + " successfully unlocked.";
}
else
{
tbMessageLine.Text = "";
}
rc = true;
}
else
{
var jsonResult2 = JsonConvert.DeserializeObject(response.Content);
string strMessage = jsonResult2.Exception.Message;
tbMessageLine.Clear();
tbMessageLine.Text = strMessage;
}
}
else
{
var jsonResult2 = JsonConvert.DeserializeObject(response.Content);
tbMessageLine.Text = jsonResult2.ToString();
}
return (rc);
}
** Snippet 3: Retrieve The Payload **
private void DownloadRemoteFile(string strRepositoryType, string strFileName, string strLocalFolder)
{
tbMessageLine.Text = "";
RestClient client = null;
RestRequest request = null;
IRestResponse response = null;
string strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/user/login";
// Password has to be hashed.
// New convention uses a Salt value from the user record
// See the Login web service for more detail
string strSalt = GetSalt(tbUserName.Text);
string strPassword = tbPassword.Password.ToString();
string strHashedPassword = HashPassword(strPassword, strSalt);
string strParms = "{\n\t\"user_name\" : \"";
strParms += tbUserName.Text;
strParms += "\",\n\t\"password\" : \"";
strParms += strHashedPassword;
strParms += "\"\n}";
client = new RestClient(strURL);
request = new RestRequest(Method.POST);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("Login", strParms, ParameterType.RequestBody);
response = client.Execute(request);
if (response.IsSuccessful)
{
var jsonResult1 = JsonConvert.DeserializeObject(response.Content);
string token = jsonResult1.access_token;
strURL = null;
client = null;
request = null;
response = null;
strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/payloads/" + strRepositoryType + "/" + strFileName;
client = new RestClient(strURL);
request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("access_token", token);
request.AddHeader("Content-Type", "text/plain");
response = client.Execute(request);
if (response.IsSuccessful)
{
// Note: Do not try to use the LoadXml API as it does not account for the encoding preamble.
XmlReader myXmlReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(response.Content)));
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(myXmlReader);
xmlDoc.Save(strLocalFolder + "\\" + strFileName);
}
else
{
var jsonResult2 = JsonConvert.DeserializeObject(response.Content);
string strMessage = jsonResult2.Exception.Message;
tbMessageLine.Text = strMessage;
}
}
else
{
var jsonResult2 = JsonConvert.DeserializeObject(response.Content);
tbMessageLine.Text = jsonResult2.ToString();
}
}
** Snippet 4: Delete The Remote Payload **
private bool DeleteRemoteFile(string strRepositoryType, string strFileName)
{
tbMessageLine.Text = "Deleting remote file " + strFileName;
bool rc = false;
RestClient client = null;
RestRequest request = null;
IRestResponse response = null;
string strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/user/login";
// Password has to be hashed.
// New convention uses a Salt value from the user record
// See the Login web service for more detail
string strSalt = GetSalt(tbUserName.Text);
string strPassword = tbPassword.Password.ToString();
string strHashedPassword = HashPassword(strPassword, strSalt);
string strParms = "{\n\t\"user_name\" : \"";
strParms += tbUserName.Text;
strParms += "\",\n\t\"password\" : \"";
strParms += strHashedPassword;
strParms += "\"\n}";
client = new RestClient(strURL);
request = new RestRequest(Method.POST);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("Login", strParms, ParameterType.RequestBody);
response = client.Execute(request);
if (response.IsSuccessful)
{
var jsonResult1 = JsonConvert.DeserializeObject(response.Content);
string token = jsonResult1.access_token;
strURL = null;
client = null;
request = null;
response = null;
strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/payloads/" + strRepositoryType + "/" + strFileName;
client = new RestClient(strURL);
request = new RestRequest(Method.DELETE);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("access_token", token);
response = client.Execute(request);
if (response.IsSuccessful)
{
tbMessageLine.Text = "Remote file " + strFileName + " successfully retrieved. Remote file deleted.";
rc = true;
}
else
{
var jsonResult2 = JsonConvert.DeserializeObject(response.Content);
string strMessage = jsonResult2.Exception.Message;
tbMessageLine.Text = strMessage;
}
}
else
{
var jsonResult2 = JsonConvert.DeserializeObject(response.Content);
tbMessageLine.Text = jsonResult2.ToString();
}
return (rc);
}
**Comment 2:**
A nice utility for experimenting with web service calls is POSTMAN. You can download a free version of POSTMAN from the following URL: https://www.getpostman.com/apps