Skip to end of banner
Go to start of banner

Converting a cURL script into a SIL script

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Current »

Converting a cURL script into a SIL script can be time-consuming. Make the conversion easy by following these steps!

Instructions

For this example, I will be building off this page, “Running Scripts Via REST”.

  1. Import the curl script into Postman by following these instructions.

  2. In the right-hand side of Postman, click on the “</>” symbol to see the code converter.

  3. Select the language you would like to convert. “Java - Unirest” is the easiest because it parses the headers in a way that is easiest to refactor into SIL code.

  4. Create headers based on the Java code. The following copied from the Java code…

    .header("Accept", "application/json")
    .header("Content-Type", "application/json")
    .header("Authorization", "Basic PFVTRVJOQU1FOlBBU1NXT1JEPg==")

    …can be converted into:

    HttpRequest request;
    request.headers += httpCreateHeader("Accept", "application/json");
    request.headers += httpCreateHeader("Content-Type", "application/json");
    request.headers += httpCreateHeader("Authorization", "Basic PFVTRVJOQU1FOlBBU1NXT1JEPg==");
  5. Now set a string variable using the body. You should be able to paste the string without modification.

    string payload = "{ \n  \"source\": {\n    \"type\": \"FILE\", \"code\": \"<PATH/TO/FILE.SIL>\"\n  },\n    \"args\": [<\"ARGUMENTS\", \"FOR\", \"ARGV\", \"GO HERE\">]\n  }";
  6. Lastly, convert the Java post() method to httpPost():

    httpPost("http://localhost:8080/rest/keplerrominfo/refapp/latest/async-script/runScript", request, payload);

Final Script

Now that we have all the basic building blocks for a script, put them all together and add some error message handling at the end of the script. If you would like to change up the values passed by the payload, see this documentation on working with JSON.

string payload = "{ \n  \"source\": {\n    \"type\": \"FILE\", \"code\": \"createIssue.sil\"\n  },\n    \"args\": [\"EX\", \"\", \"Task\", \"summary goes here\"]\n  }";

HttpRequest request;
request.headers += httpCreateHeader("Accept", "application/json");
request.headers += httpCreateHeader("Content-Type", "application/json");
request.headers += httpCreateHeader("Authorization", "Basic PFVTRVJOQU1FOlBBU1NXT1JEPg==");

string value = httpPost("http://localhost:8080/rest/keplerrominfo/refapp/latest/async-script/runScript", request, payload);

runnerLog(value);

number statusCode = httpGetStatusCode();
if (statusCode >= 200 && statusCode < 300) {
    runnerLog("SUCCESS");
}
else {
    string msg = trim(statusCode) + ":" + httpGetErrorMessage() + ":" + httpGetReasonPhrase();
    runnerLog(msg);
}
  • No labels