Converting a cURL script into a SIL script

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():

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.