Debugging a Failed API Request, and Defining an Authorization Header Using Fiddler Everywhere

November 07, 2020

Postman is a really great tool, but I’ve recently been playing with Telerik’s new version of Fiddler - Fiddler Everywhere. You’re probably thinking that these are separate tools, that do separate jobs… and before Fiddler Everywhere, you’d have been right. However, have a look at this screen:

fiddler 1

…In fact it’s not Postman. The previous version of this tool (called Compose) from Fiddler 4 was pretty clunky - but now you can simply right-click on a request and select “Edit in Compose”.

Use Case

Let’s imagine for a minute, that you’ve made a request, and you got a 401 because the authentication header wasn’t set. You can open that request in Fiddler:

fiddler 2

In fact, this returns a nice little web snippet, which we can see by selecting the Web tab:

fiddler 3

The error:

The request requires HTTP authentication

This error means that authentication details have not been passed to the API; typically, these can be passed in the header, in the form:



Authorization: Basic EncodedUsernamePassword

So, let’s try re-issuing that call in Fiddler - let’s start with the encoding. Visit this site and enter into the Encode section your username and password:



Username:password

For example:

fiddler 4

In Fiddler, right click on the request in question, and select to Edit in Compose. You should now see the full request, and be able to edit any part of it; for example, you can add an Authorization header:

fiddler 5

Now that you’ve proved that works, you can make the appropriate change in the code - here’s what that looks like in C#:



            byte[] bytes = Encoding.UTF8.GetBytes($"{username}:{password}");
            var auth = Convert.ToBase64String(bytes);

            var client = \_httpClientFactory.CreateClient();
            client.DefaultRequestHeaders.Add($"Authorization", $"Basic {auth}");



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024