Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDWebImage and setting custom HTTP headers?

Tags:

I´ve just changed my code for caching images away from EGOImageCache to SDWebView. Unfortunately i don´t know how to set custom HTTP headers as i have to send authentification to be able to fetch images. It was easy done with EGOImageCache as i´ve extended the NSURLRequest at the appropriate place. But i don´t know how to do that with the SDWebView.framework. I see the headers and i´ve found methods in SDWebImageDownloader.h containing

    /**  * Set a value for a HTTP header to be appended to each download HTTP request.  *  * @param value The value for the header field. Use `nil` value to remove the header.  * @param field The name of the header field to set.  */ - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;  /**  * Returns the value of the specified HTTP header field.  *  * @return The value associated with the header field field, or `nil` if there is no corresponding header field.  */ - (NSString *)valueForHTTPHeaderField:(NSString *)field; 

It seems that the lib does support HTTP headers. But as i use UIImageView+WebCache.h i can´t see there an option for setting the headers. In my code i call

[self.imageView setImageWithURL:[NSURL URLWithString:themeImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

Can anybody tell me how to set HTTP headers?

like image 596
rockstarberlin Avatar asked Apr 28 '13 18:04

rockstarberlin


People also ask

Can HTTP headers be custom?

There are many uses for custom headers and they are quite commonly used. Even if you aren't using a CDN or haven't specifically defined any custom HTTP headers on your origin server, you may still be sending responses with custom headers.

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

What is custom header in API?

Custom Headers allow us to add extra content to our HTTP requests and responses, which we can pass between the client and server. We can use custom headers for metadata, such as defining the current version of the API that is being used.


1 Answers

I had the same problem, and I tried to make:

SDWebImageDownloader *manager = [SDWebImageDownloader sharedDownloader]; [manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"]; 

But the header were not send. After some tries, I came across the problem, SDWebImageDownloader at sharedDownloader makes a new instance of SDWebImageDownloader, so when you put the header at that instance, the instance that really downloads the image don't has the header.

I've solved making this:

SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader; [manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"]; 
like image 138
Néstor Avatar answered Oct 07 '22 00:10

Néstor