Sdwebimage: modifying the picture load request to add headers

Created on 16 Aug 2012  路  12Comments  路  Source: SDWebImage/SDWebImage

Hi there,

as far as I can see, SDWebimage does not allow to modify the http-request used for getting images. The start method of SDWebimageDownloader just creates the request and there is no way to do things like adding headers. But there are many good reasons why you could want to add http-request-header, for example:

  • the image is loaded from amazon aws S3 and you need an authentication header because the images are private
  • any other custom or standards-based http-authentication schemes
  • cookies
  • the image is not static but generated by a rest or webservice that reacts on certain custom headers
  • ...I think there are more.

Is there a proper way to add headers to the NSURLRequest before it get's scheduled? Or do I have to overwrite the SDWebImageDownloader?

thanx
No Gece

Most helpful comment

I have tried this, but it doesn't work

SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
[manager setValue:[NSString stringWithFormat:@"Basic %@",authValue] forHTTPHeaderField:@"Authorization"];

Is there any solution to do basic authorization with SDWebImage lib?

All 12 comments

A way to set the HTTP headers would be a great addiction, or a way to expose the NSURLRequest. I have to add a token on http header to get the picture, so I have to hard code this in the SDWebImage code.

Hi aleufs how did you go about doing this? Could you give me some guidance?

I've added my code accessing the NSURLRequest on line 79 of https://github.com/rs/SDWebImage/blob/master/SDWebImage/SDWebImageDownloader.m. We have to hard code this because this variable is a method variable and we don't have access to it outside of the method.

https://github.com/jdp-global/SDWebImage.git

#import <SDWebImage/SDWebImageDownloader.h>

+ (void)setDefaultHeader:(NSString *)header value:(NSString *)value

   // inject an authorisation header
        [SDWebImageDownloader setAuthorizationHeaderWithUsername:auth_token password:@""];

  // set a custom header
    [SDWebImageDownloader setDefaultHeader:@"foo" value:@"bar"];


  // you'll need to clear it when not needed!!!!
        [SDWebImageDownloader clearAuthorizationHeader];

          [documentView setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:SDWebImageProgressiveDownload
                          success:^(UIImage *sdImage){

                          }failure:^(NSError *error){

                                  DLog(@"error:%@",error);
                          }];

I merged code from Gowalla classes - so had to include their license too.

Thanks! However, I doubt that this will work with AWS. The Auth-header in REST-authentication a la AWS is different for each and every request.

You can keep overwriting the header value b4 each image request.
I added a method for auth token in the download class. Try it.

Oh actually that sucks as it is static method - it will break with multiple requests.

I found a better solution ouside of SDWebImage: intercepting the requests by registering my own URLProtocol.

Although this was closed without any reason. But i think this feature request is still valid and needed.

I have tried this, but it doesn't work

SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
[manager setValue:[NSString stringWithFormat:@"Basic %@",authValue] forHTTPHeaderField:@"Authorization"];

Is there any solution to do basic authorization with SDWebImage lib?

That worked for me :)

@nogece would you share your approach with URLProtocol?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohacs picture mohacs  路  5Comments

paul-lavoine picture paul-lavoine  路  4Comments

floprr picture floprr  路  4Comments

doingy picture doingy  路  4Comments

halilyuce picture halilyuce  路  4Comments