Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

TestRail MiniAPI: Magic quotes and other special characters


#1

I’m using the TestRail MiniAPI to batch change some of fields (like custom_casenotes, custom_steps or other custom_fields) and sometimes they contain special characters like magic quotes ( ” “ ’ ‘ )

Everytime I post my request, the TestRail Mini API ends up deleting the magic quote and anything after it.

For example:

This is the PHP code I use to interact with your mini API.

                $url = "http://$httphost/testrail/index.php?/miniapi/update_case/$case_id&key=$key;

                $response = post_request($url, $data, 'POST', 'json', $verbose);

Details on the post_request function:

function post_request($url, $params = null, $verb = 'GET', $format = 'json', $verbose = 1) {
        if ($verbose) {
                echo "<br><font face=verdana size=2><br><br>Creating POST request<br><hr size=1>";
                echo "URL: $url<br>";
                echo "Data:<br><xmp>";
                print_r($params);
                echo "</xmp><hr size=1><br><br>";
        }

        $cparams = array(
                'http' => array(
                'method' => $verb,
                'ignore_errors' => true
                )
        );

        if ($params !== null) {
                $params = http_build_query($params);

                if ($verb == 'POST') {
                        $cparams['http']['content'] = $params;
                } else {
                        $url .= '?' . $params;
                }
        }

        $context = stream_context_create($cparams);
        $fp = fopen($url, 'rb', false, $context);

        if (!$fp) {
                $res = false;
        } else {
                // If you're trying to troubleshoot problems, try uncommenting the
                // next two lines; it will show you the HTTP response headers across
                // all the redirects:
                // $meta = stream_get_meta_data($fp);
                // var_dump($meta['wrapper_data']);
                $res = stream_get_contents($fp);
        }

        if ($res === false) {
        throw new Exception("$verb $url failed: $php_errormsg");
        }

        switch ($format) {
                case 'json':
                        $r = json_decode($res);

                        if ($r === null) {
                                throw new Exception("failed to decode $res as json");
                        }

                        if ($verbose) {
                                echo "Acquired POST response:<br><hr size=1><xmp>";
                                print_r($r);
                                echo "</xmp><hr size=1>";
                        }

                        return $r;

                case 'xml':
                        $r = simplexml_load_string($res);

                        if ($r === null) {
                                throw new Exception("failed to decode $res as xml");
                        }

                        return $r;
                }

        return $res;
}

// TestRail MINI API returns a json response that needs to be parsed/decoded.
// A PHP json_decode function exists in PHP 5.2, but we currently have PHP 5.1 installed
// If we ever upgrade to PHP 5.2+, this function can be removed.

function json_decode($json)  {
        $comment = false;
        $out = '$x=';

        for ($i=0; $i<strlen($json); $i++) {
                if (!$comment) {
                        if ($json[$i] == '{' || $json[$i] == '[') $out .= ' array(';
                        else if ($json[$i] == '}' || $json[$i] == ']') $out .= ')';
                        else if ($json[$i] == ':') $out .= '=>';
                        else   $out .= $json[$i];
                } else $out .= $json[$i];

                if ($json[$i] == '"') $comment = !$comment;
        }

        eval($out . ';');
        return $x;
}

I am wondering… maybe TestRail requires the POST request to be formatted with a specific encoding in order to parse magic quotes correctly? We are using the following versions:

Thanks in advance for your feedback :slight_smile:


#2

Hello Adron,

Thanks for your posting. This may be a Unicode issue. In which string encoding do you submit the values? You need to make sure to use the UTF-8 encoding if you use characters that are not part of the standard 7-bit range (which is the case with ” “ characters).

I hope this helps and just let me know in case you have any questions.

Regards,
Tobias