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

Test rail API throwing 401 Unauthorized error even with correct authorization

I’m trying to establish a connection to test rail and passing the correct authorization but it throws 401 Unauthorized error.

The same code was works on a different test rail but is not working with new instance.

I have enabled API integration in test rail settings and generated the API Authorization key. Is there anything else that I need to take care of to solve this issue?

public static void SendHttpRequest(String jsonStr, String runId)

            String URL = prop.getProperty("TEST_RAIL_URL") + runId;  
            //TEST_RAIL_URL - https://testrail.xxxxxx.com/index.php?/api/v2/add_results_for_cases/
            java.net.URL urlObj = new java.net.URL(URL);

            HttpsURLConnection con = null;
            con = (HttpsURLConnection) urlObj.openConnection();
            con.setRequestMethod(prop.getProperty("API_METHOD"));
            //API_METHOD = POST
            con.setRequestProperty("Content-Type", prop.getProperty("API_CONTENT_TYPE"));
            //API_CONTENT_TYPE = application/json
            con.setRequestProperty("Authorization", prop.getProperty("API_AUTHORIZATION"));
            //API_AUTHORIZATION = Basic <base 64 encoded api key>

            System.out.println("Test Rail connection ready");
            System.out.println(con);
            con.setDoOutput(true);

            DataOutputStream outObj = new DataOutputStream(con.getOutputStream());
            outObj.writeBytes(jsonStr);
            outObj.flush();
            outObj.close();
            System.out.println("API request made to Test Rail");

Console output:

Test Rail connection ready
sun.net.www.protocol.https.DelegateHttpsURLConnection:https://testrail.xxxxxx.com/index.php?/api/v2/add_results_for_cases/56
API request made to Test Rail
Dec 24, 2019 9:37:17 PM com.testrail.integration.TestRailInt SendHttpRequest
INFO: 401Unauthorized
Dec 24, 2019 9:37:17 PM com.testrail.integration.TestRailInt SendHttpRequest
INFO: {"error":"Authentication failed: invalid or missing user\/password or session cookie."}
401Unauthorized
{"error":"Authentication failed: invalid or missing user\/password or session cookie."}
Dec 24, 2019 9:37:17 PM com.testrail.integration.TestRailInt main
INFO: Enter finally. Closing

I’m guessing its something to do with test rail setup (admin access) or network level issue or probably authorization header getting stripped.

Hoping someone can point me in the right direction. TIA

Hi Varun,

The 4xx error messages do indicate an issue on the client-side. Based on your description, review the credentials being used to ensure the username is accurate and possibly generate a new API key to be certain. Make sure to click “Save Settings” after generating the API key.

As for a cookie session issue, this will normally go away if you run the request agian.