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/
Curl string syntax in 2018 r3
Re: Curl string syntax in 2018 r3
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
https://www.enfocus.com/manuals/Develop ... class.html
Re: Curl string syntax in 2018 r3
I quickly adjusted a sample from the documentation to do this:
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.
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);
}
Edit: Adjusted the sample to use the http headers in the curl example.
Re: Curl string syntax in 2018 r3
Thanks for your help.
I will try this to see if I can get this to work.
Ron
I will try this to see if I can get this to work.
Ron
Re: Curl string syntax in 2018 r3
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/ ";
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/ ";