returning json from fly machine api

I am using fly machine to run student’s code in an alpine image. I have created a UI where they can upload their c++ code.

I have my admin area, where I can upload some helper files like shellscript and txt files which i use to compare student’s output to mine. Here is how my script looks.

output=$(./executable 1 < input.txt)
expected_output=$(cat solution.txt)
if [ "$output" = "$expected_output" ]; then
JSON_FMT='{"state": "%s", "output":"%s"}'

printf "$JSON_FMT" "$state" "$output"

using the rest api I make a POST req to container/machines/#{machine_id}/exec with cmd: “sh ./” and i get back (just an example)

   "exit_code": 0,
   "exit_signal": 0,
   "stderr": "",
   "stdout": "{\"state\": \"pass\", \"output\":\"1 33 55\n 55 77 88\n \"}"

as you can see, the stdout is not a valid json because of \n or linefeed. So in your opinion, how should i handle this case? Is there other way than sending json so that I can know the code passed the comparison and i can read the output?

In cases like this, it is common to use Base64—an encoding that eschews all interesting symbols…

On Debian, it’s available in the coreutils package, but I don’t know about Alpine:

$ echo -e '1 33 55\n 55 77 88' | gzip -c | base64 --wrap=0

   base64 --decode | gunzip -c
1 33 55
 55 77 88

The reason for the gzip is its inherent error detection. It didn’t actually improve the size…

   base64 --decode | gunzip -c
1 33 55
gzip: stdin: unexpected end of file

Only half right!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.