Post XML with http request

Post Reply
arjen@publ-ict.nl
Newbie
Posts: 3
Joined: Tue Jul 12, 2016 10:10 am

Post XML with http request

Post by arjen@publ-ict.nl »

Hi,

I do have the need to send and XML file to a webserver with http request.

Question is, How?; )

The XML (basically a log file, I want to process on the webserver) will be send through a Switch flow and at some point it must arrive on the webserver.
Basic question is how to get the file that is processed by switch uploaded to the webserver.

The configurator has functionality to attach a "fixed" file, but that't not what I want.

I do have a url (entrypoint), that already allows me to communicate with the webserver.

All tips and examples are welcome.
sander
Advanced member
Posts: 276
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: Post XML with http request

Post by sander »

I used this to post a JMF to Production Center, the original script is found on crossroads which I edited a little. My edit's can be done better, but this was my first scripting experience.

If you replace the JMF/XML part with your needs, I bet that will work.

Code: Select all

// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{	
	
/*
<?xml version="1.0" encoding="UTF-8"?>
<JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" SenderID="Switch Send JMF" TimeStamp="2015-05-18T13:51:16" Version="1.4">
  <Command ID="qid1431949876717" Type="SubmitQueueEntry">
    <QueueSubmissionParams URL="http://appl01:8083/jdf/Switch/Switch.jdf" ReturnJMF="http://appl01:8083/jmf"/>
  </Command>
</JMF>
*/
	//Create JMF XML	
	jmfTempLoc = s.createPathWithName(job.getVariableAsString("[Job.NameProper]") + ".jmf");
	var jmfDoc = new Document();
	//Create map for various namespaces
	var jdfMap = jmfDoc.createEmptyMap();
	jdfMap.put("","http://www.CIP4.org/JDFSchema_1_1");
	
	var jmfElem = jmfDoc.createElement("JMF", jdfMap);
	jmfDoc.setDocumentElement(jmfElem);
	jmfElem.addAttribute("xmlns", jdfMap, "http://www.CIP4.org/JDFSchema_1_1");
	jmfElem.addAttribute("SenderID", jdfMap, "Switch Send JMF");
	var time = new Date();
	jmfElem.addAttribute("TimeStamp", jdfMap, time.toString());
	jmfElem.addAttribute("Version", jdfMap, "1.4");
	
	var queryElem = jmfDoc.createElement("Command");
	var queryID = "qid" + time.getTime().toString();
	queryElem.addAttribute("ID", jdfMap, queryID);
	jmfElem.appendChild(queryElem);
	
	queryElem.addAttribute("Type", jdfMap, "SubmitQueueEntry");

	var statusQuParamsElem = jmfDoc.createElement("QueueSubmissionParams");	
	queryElem.appendChild(statusQuParamsElem);
	statusQuParamsElem.addAttribute("URL", null,"http://appl01:8083/jdf/Switch/" + job.getVariableAsString("[Job.Name]"));
	statusQuParamsElem.addAttribute("ReturnJMF", null, "http://appl01:8083/jmf");
	
	jmfDoc.save(jmfTempLoc);
	
	s.copy(job.getPath(), "\\\\appl01\\JDF\\JDFPickup\\Switch\\" + job.getVariableAsString("[Job.Name]"));
			
	var jobJMF = s.createNewJob();	
	
	//Setup http method for sending JMF message and collecting response	
	var httpClient = new HTTP();
	httpClient.url = "http://hpprocenter:8080/jmf/HPProCenter";
	httpClient.setAttachedFile(jmfTempLoc);
	var responsePath = jobJMF.createPathWithName(job.getVariableAsString("[Job.NameProper]") + ".xml");
	httpClient.setLocalFilePath(responsePath);
	httpClient.addHeader("Content-Type", "application/vnd.cip4-jmf+xml");
	httpClient.addHeader("charset", "UTF-8");
	httpClient.post();
	
	while (!httpClient.waitForFinished( 1 )) {
	//no need to sleep here, the 1 in the waitForFinished function sets the delay time (in seconds)
	//You could send log updates here to show progress if needed or check against a timeout value.
	}
	if (httpClient.finishedStatus == HTTP.Ok){
		s.log(1, "HTTP response was: " + httpClient.statusDescription + " with additional messages: " + httpClient.lastError);
		jobJMF.sendToData(1, responsePath);
		job.sendToData(1, job.getPath());
		job.sendToData(1, jmfTempLoc);
	}
	else {
		s.log(2, "Issue with JMF query. HTTP Status = " + httpClient.finishedStatus + " Last Error = " + httpClient.lastError );
		jobJMF.sendToData(3, responsePath);
		job.sendToData(3, job.getPath());
		job.sendToData(3, jmfTempLoc);
	}
//	job.sendToSingle(jmfTempLoc);
//	job.sendToSingle(job.getPath());
}
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

Re: Post XML with http request

Post by cstevens »

JMF has a special Content-Type. For standard XML you'll probably want to use:

httpClient.addHeader("Content-Type", "application/xml");

Other than that just make sure that the httpClient.setAttachedFile(location) is pointing to the location of your XML file.
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: Post XML with http request

Post by freddyp »

With the "HTTP request" element you can send the input file by setting "Attached file" to "Single-line text with variables" and choosing the variable [Job.Path].

You will have to use POST as the method, and you will have to experiment if you have to use "Yes" or "No" for "Enable MIME encoding".
arjen@publ-ict.nl
Newbie
Posts: 3
Joined: Tue Jul 12, 2016 10:10 am

Re: Post XML with http request

Post by arjen@publ-ict.nl »

freddyp wrote:With the "HTTP request" element you can send the input file by setting "Attached file" to "Single-line text with variables" and choosing the variable [Job.Path].

You will have to use POST as the method, and you will have to experiment if you have to use "Yes" or "No" for "Enable MIME encoding".
Thanks Freddy, had already found it, but it's always good to have the confirmation.
Post Reply