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

What are the limitations on using the API from Javascript?

I’ve been trying to call the API using AJAX from a Confluence page with no luck. I’m starting to think that the API can only be called from a Testrail domain web page. Is this true?

I hope it is not the truth, but same query from my side too. I tried and got no result as of now till date.

Regards,
Budre Petre

@budrepetre It works. You just have to get CORS setup correctly with the Testrail server. We use Apache and here is how I configured our httpd.conf:

#### Begin CORS settings ####
SetEnvIf Origin ".+$" AccessControlAllowOrigin=$0
Header always set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header merge Vary Origin

Header always set Access-Control-Allow-Methods: "POST, GET, OPTIONS"
Header always set Access-Control-Allow-Headers: "Accept,Origin,Authorization,Content-Type,Referer,User-Agent,X-Requested-With,x-atlassian-mau-ignore"
Header always set Access-Control-Allow-Credentials: "true"

# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
#### End CORS settings ####

And from your client you must send the header:
X-Requested-With: API Client
and if using JQuery ajax(), send this option:
xhrFields: {
withCredentials: true
}

1 Like