Judge0 • Some Attributes for This Submission Cannot Be Converted to UTF-8

by hermanzdosilovic
GNU/Linux ◆ xterm-256color ◆ bash 3370 views

If you ever got this error when using Judge0 keep reading:

{
	"error": "some attributes for this submission cannot be converted to UTF-8, use base64_encoded=true query parameter"
}

This simple and valid Python code can cause the above error:

print("\xFE")

What happened? The above, completely valid, Python code outputs a character sequence whose hex value is FE and that is not a valid UTF-8 character sequence. Thus, the output of the above code cannot be received in a JSON format that has plain text values but rather it needs to be encoded somehow.

Judge0 allows you to send and receive Base64 encoded data. This allows you to send and receive content that cannot be encoded with UTF-8.

The example from this asciicast shows that the submission was created with raw data, i.e. Base64 encoding was not used. But to successfully receive submission results Base64 encoding had to be used (notice the base64_encoded=true).

Because you don’t know what code your users might run you should always use Base64 when interacting with the Judge0. Your users should be able to run the above and similar code snippets.

To help you more with this, you might find the following snippet useful:

function encode(str) {
    return btoa(unescape(encodeURIComponent(str || "")));
}

function decode(bytes) {
    var escaped = escape(atob(bytes || ""));
    try {
        return decodeURIComponent(escaped);
    } catch {
        return unescape(escaped);
    }
}

These are the Javascript functions that Judge0 IDE uses for encoding and decoding data on a client-side.