Adding headers to image request in Glide

Paulina Sadowska
3 min readSep 4, 2016

--

During Android app development it’s safe to assume that sooner or later you will need to fetch and display images from the Web. Fortunately there are many libraries out there which allow us to make it in the easy and very efficient way.

My recent favorite is Glide library, which even happens to be recommended by Google. And I’m not really surprised by this fact: it’s fast, powerful and easy to use. Especially if you used another image loader library: Picasso.

I don’t want to write about installing Glide and how to use their basic methods because I saw that topic covered many times (e.g. here or here). But I want to share with you very cool way Glide allows us to add headers to the request.

So, lets the fun begin!

source

Making a request for an image in Glide without a header it’s a piece of cake. We simply call:

And that’s it.

But let’s assume our API requires some Authorization header. How we can add it to our request?

The “simplest” (but I will rather call it the laziest) approach can look something like this:

We use GlideUrl class, create new object with our url address as an parameter and add headers to it.

Then we can get this GlideUrl object and pass it to the load method just like this:

And it’s working! Thats it?

Well… not so fast.

There are few problems with this approach:

First and foremost: GlideUrl class was not meant to be used that way (source).

Let’s assume we don’t use hardcoded url address but we fetch it from the API. And we all know that APIs are the evil things that should not be trusted.

source

So, our evil API at some point decided it doesn’t like us anymore and it sent us an empty string instead of image address. Or just null.

In this case our users will run into this dialog:

And I’m sure that’s nobody’s favorite dialog.

Second of all:

I really like keeping things as simple as possible and I hate the fact that I have to write anything else than just an url address in the load() method.

And fortunately Glide provides a really great alternative!

Modules

Modules are the way to globally change Glide behavior and are just perfect for adding request headers.

To create it we have to follow this few steps:

1 Create GlideModuleWithHeaders class which implements GlideModule interface:

2 Register your module in AndroidManifest.xml file (make sure you paste it between <application> tags and edit android:name).

3 If you use ProGuard you have to also add following lines to your *.pro file:

4 Create HeaderLoader class which will take care of creating your headers.

As you can see from the snippet I made REQUEST_HEADERS private static final field in HeaderLoader class but you can also create Headers directly in getHeaders method.

5 You also need to create factory class for HeaderLoader to be able to register it later in our Glide module.

6 And the last step: register your HeaderLoader in registerComponent method of GlideModuleWithHeaders class:

And now we can make request with headers passing only url address to load() method. Just like we did this when the API didn’t need any headers. They will be added behind the scene.

If in doubt please check out my demo project on Github. If you have any questions or suggestions, please let me know!

--

--