Curl string syntax in 2018 r3

Post Reply
ron_ag
Newbie
Posts: 3
Joined: Wed May 22, 2019 9:13 pm

Curl string syntax in 2018 r3

Post by ron_ag »

Hi,

First time using Switch 2018. I am having issues with getting a curl string to work directly from Switch. Script fails with json syntax errors.

BUT If I copy the message log field from the job.log of the "theString" to capture what Switch is passing to cmd The string works correctly when copied into cmd line manually. But it won't work directly from Switch. Any thoughts?

curl command in SwitchScripter

var theString = "c:\\curl\\bin\\curl -H \"test-upp-api-key: 12345r6788\" -H \"test-upp-api-version: v2\" -H \"Cache-Control: no-cache\" -H \"Content-Type: application/json\" -X POST -d \"{\\\"date_time\\\": \\\"2019-05-21 10:56:08\\\", \\\"order_id\\\": \\\"1234-1.1\\\", \\\"type\\\": \\\"PRODUCTION_STARTED\\\"}\" https://api.playbox.test.com/printer-signals/ ";

The below is the above command when copied from the job.log message of the above variable. If the below is copied into cmd it works correctly.

c:\curl\bin\curl -H "test-upp-api-key: 12345r6788" -H "test-upp-api-version: v2" -H "Cache-Control: no-cache" -H "Content-Type: application/json" -X POST -d "{\"date_time\": \"2019-05-21 10:56:08\", \"order_id\": \"1234-1.1\", \"type\": \"PRODUCTION_STARTED\"}" https://api.playbox.test.com/printer-signals/
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Curl string syntax in 2018 r3

Post by Padawan »

Are you aware that you can do http requests directly in Switch scripting? This way you don't have the escape characters nightmare which you have when building a cli command with curl.

https://www.enfocus.com/manuals/Develop ... class.html
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Curl string syntax in 2018 r3

Post by Padawan »

I quickly adjusted a sample from the documentation to do this:

Code: Select all

function timerFired( s : Switch )
{
	var requestProps = {};
	requestProps.date_time = "2019-05-21 10:56:08";
	requestProps.order_id = "1234-1.1";
	requestProps.type = "PRODUCTION_STARTED";
	
	
	var theHTTP = new HTTP();
	
	theHTTP.setAttachedFile( createFilePathWithContent( s, JSON.stringify(requestProps) ) );
	theHTTP.url = "https://api.playbox.test.com/printer-signals/";
	theHTTP.addParameter( "test-upp-api-key" , "12345r6788" );
	theHTTP.addParameter( "test-upp-api-version" , "v2" );
	theHTTP.addParameter( "Cache-Control" , "no-cache" );
	theHTTP.addParameter( "Content-Type" , "application/json" );
	
	theHTTP.post();
	
	while( !theHTTP.waitForFinished( 3 ) )
	{
		s.log( 1, "Upload in progress" );
	}
	
	var theFinishedStatus = theHTTP.finishedStatus;
	var theServerResponse = theHTTP.getServerResponse().toString();
	
	if( theFinishedStatus == HTTP.Ok  )
	{
		s.log( 1, "Server response: %1", theServerResponse );
		s.log( 1, "Upload completed successfully" );
	}
	else if( theFinishedStatus == HTTP.Failed  )
	{	
		s.log( 1, "Server response: %1", theServerResponse );
		s.log( 3, "Upload  failed with the error: %1", theHTTP.lastError );
	}
	else
	{
		s.log( 2, "Unknown error" );
	}
	
}
function createFilePathWithContent( s : Switch , content)
{
	var theFilePath = s.createPathWithName( "temp.txt" );	
	var theFile = new File( theFilePath );
	theFile.open( File.WriteOnly );
	theFile.write(content)
	theFile.close();	
	// Return the path to the file with native path separators
	return Dir.convertSeparators(theFile.fullName);	
}
The response is in the theServerResponse variable. Looks like it is html.

Edit: Adjusted the sample to use the http headers in the curl example.
ron_ag
Newbie
Posts: 3
Joined: Wed May 22, 2019 9:13 pm

Re: Curl string syntax in 2018 r3

Post by ron_ag »

Thanks for your help.

I will try this to see if I can get this to work.

Ron
ron_ag
Newbie
Posts: 3
Joined: Wed May 22, 2019 9:13 pm

Re: Curl string syntax in 2018 r3

Post by ron_ag »

Hi,

Sorry for the delay. I could not get that way to work. I did get the traditional way to work and its listed below. Note that the variable is now used instead of the hard coded value.

var theString = "c:\\curl\\bin\\curl -i -H \"test-upp-api-key: 12345r6788\" -H \"test-upp-api-version: v2\" -H \"Cache-Control: no-cache\" -H \"Content-Type: application/json\" -X POST -d \"{\"date_time\": \"" + pborderdate + "\", \"order_id\": \"" + _orderid + "\", \"type\": \"PRODUCTION_STARTED\"}\" https://api.playbox.test.com/printer-signals/ ";
Post Reply