SDWebImage / SDWebImage

Asynchronous image downloader with cache support as a UIImageView category
https://sdwebimage.github.io
MIT License
25.05k stars 5.96k forks source link

modifying the picture load request to add headers #166

Closed nogece closed 12 years ago

nogece commented 12 years ago

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:

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

aleufms commented 12 years ago

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.

bhaity commented 12 years ago

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

aleufms commented 12 years ago

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.

johndpope commented 12 years ago

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.

nogece commented 12 years ago

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.

johndpope commented 12 years ago

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

johndpope commented 12 years ago

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

nogece commented 12 years ago

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

itsSaad commented 9 years ago

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

abraxascorner commented 8 years ago

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?

judepereira commented 8 years ago

That worked for me :)

farzadshbfn commented 7 years ago

@nogece would you share your approach with URLProtocol?