Automation Testing

Article · 2 min read

UFT API TEST RETURNS HTTP 401 ERROR

Server returned HTTP status code 401: Since publishing my UFT book The UFT API Testing Manifesto – A step-by-step, hands-on […]

Joe Colantonio

By Joe Colantonio

Founder, TestGuild · 22 September 2013 at 12:20 PM · Updated 05 January 2025 at 1:28 PM

UFT API TEST RETURNS HTTP 401 ERROR

Server returned HTTP status code 401:

Since publishing my UFT book The UFT API Testing Manifesto – A step-by-step, hands-on testing guide for the masses, I’ve received a few emails from folks who are receiving this error message: Server returned HTTP status code 401: Unauthorized while attempting to run their API tests.

More about the 401 Unauthorized error

According to Wikipedia, the 401 Unauthorized message is “401 Unauthorized is similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided.[2] The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.”

Why this 401 error occurs in UFT API

Sometimes the server you’re testing against expects to receive authentication information before transport. For this reason, you must add some additional information to your API request’s HTTP header. This can be done using custom code in BeforeApplyProtocolSettings event handler.

How to add a username and password to your HTTP Request Header

  • In your test, click on the web service activity on your main canvas area.
  • Under its Properties section, click on the Events icon.


  • Select the Create a default handler…option from the BeforeApplyProtocolSettings event.


  • In the TestUserCode.cs file, under the StServiceCallActivity5_OnBeforeApplyProtocolSettings method, add the following (note your StServiceCallActivity number might be different)
  • Create a string variable that contains the user name and password your server needs for authentication:

string myAuth = “Basic” + Convert.FromBase64String(System.Text.Encoding.ASCII.GetBytes(“YourUserName:YourPassword));

  • Next, add your authentication variable value as a parameter to the HttpRequestHeaders Add method:

this.StServiceCallActivity5.HttpRequestHeaders.Add(“Authorization”,myAuth);


  • Run your test. You should no longer receive a 401 error message.

   
More UFT API Automation Awesomeness


For more UFT API Automation Awesomeness, tips, tricks and how-tos, check out my new book The UFT API Testing Manifesto – A step-by-step, hands-on testing guide for the masses –now available on Amazon.com!

Don’t own a Kindle? No Problem — you can still read my book!

I’m aware that some of you have had trouble accessing my books in the past. The truth is that you don’t need a Kindle! There are a variety of free ways you can read a Kindle eBook:

  • Read it on your PC or Mac (Kindle Cloud Reader)
  • Read it on your iPhone or iPad (iTunes App)
  • Read it on your Android (Kindle for Android)
  • Read it on your Blackberry (Kindle for Blackberry)

In short, it’s really simple to read Amazon Kindle books, no matter what type of device you use to access the Internet.

About the author

Joe Colantonio

Joe Colantonio

Founder, TestGuild · Host, TestGuild Automation Podcast

Joe founded TestGuild in 2010 and has spent 27+ years in test automation. He hosts the longest-running automation-testing podcast (600+ episodes) and created Automation Guild, the premier online event for test automation engineers.

Joe Colantonio, founder of TestGuild

About the author

About Joe Colantonio

Joe Colantonio is the founder of TestGuild, an industry-leading platform for automation testing and software testing tools. With over 25 years of hands-on experience, he has worked with top enterprise companies, helped develop early test automation tools and frameworks, and runs the largest online automation testing conference, Automation Guild.

Joe is also the author of Automation Awesomeness: 260 Actionable Affirmations To Improve Your QA & Automation Testing Skills and the host of the TestGuild podcast, which he has released weekly since 2014, making it the longest-running podcast dedicated to automation testing. Over the years, he has interviewed top thought leaders in DevOps, AI-driven test automation, and software quality, shaping the conversation in the industry.

With a reach of over 400,000 across his YouTube channel, LinkedIn, email list, and other social channels, Joe’s insights impact thousands of testers and engineers worldwide.

He has worked with some of the top companies in software testing and automation, including Tricentis, Keysight, Applitools, and BrowserStack, as sponsors and partners, helping them connect with the right audience in the automation testing space.

Follow him on LinkedIn or check out more at his TestGuild profile .

Free PDF

Get 10 quick test automation tips

Actionable tips from top industry experts — skim them fast.