Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header to image request #51

Closed
vipatronon opened this issue Nov 11, 2019 · 10 comments
Closed

Add header to image request #51

vipatronon opened this issue Nov 11, 2019 · 10 comments
Labels

Comments

@vipatronon
Copy link

Is there a way to add a header value to the WebImage component? To get the images from my server, I must pass some info in there

Thanks in advance

@dreampiggy
Copy link
Collaborator

dreampiggy commented Nov 12, 2019

Already supported and mentioned on Readme:

URL Request / Response Modifier, provide custom HTTP Header

See the wiki, you need to pass the context option .requestModifier for single image request, or you can setup a common header (affect all requests), in the SDWebImageDownloader.shared.

Wiki : https://github.com/SDWebImage/SDWebImage/wiki/How-to-use#use-request-modifier-50

I don't want to add many code sample or details in readme, since it's for beginner and quick guide for a framework. For details, the docuementation and wikis is always the better place to check.

@dreampiggy
Copy link
Collaborator

dreampiggy commented Nov 12, 2019

You can also just use another API, but this will apply to all image requests, and does not support a block-based arg (Which means, it's static, not dynamic). But it's simpler:

https://sdwebimage.github.io/Classes/SDWebImageDownloader.html#/c:objc(cs)SDWebImageDownloader(im)setValue:forHTTPHeaderField:

@vipatronon
Copy link
Author

The wiki exemple worked perfectly!

Thank you!

@dreampiggy
Copy link
Collaborator

@vipatronon I update the readme with a new chapter called Customization and configuration setup, make it easier for new user to find the place to do something setup about HTTP headers, coder plugins. Hope this can help for you.

https://github.com/SDWebImage/SDWebImageSwiftUI#customization-and-configuration-setup

@vipatronon
Copy link
Author

@dreampiggy thank you for updating the readme, I helped me for sure and I think this will help new users too

@LilaQ
Copy link

LilaQ commented Mar 9, 2023

@dreampiggy I still don't get it. How can you add a header for a single request? Where / how do you passt the requestModifier for a single request?
All I can see is changing default headers.

@dreampiggy
Copy link
Collaborator

dreampiggy commented Mar 10, 2023

When using requestModifier, SD will pass you a URLRequest value, you can return back another URLRequest.

URLRequest (actually the mutable one) has a property called allHTTPHeaderFields

You know how to use... Though this API not really convenient for simple use case. You can wrap it into a category for convenient

@dreampiggy
Copy link
Collaborator

let requestModifier = SDWebImageDownloaderRequestModifier { (request) -> URLRequest? in
    if (request.url?.host == "foo") {
        var mutableRequest = request
        mutableRequest.setValue("foo=bar", forHTTPHeaderField: "Cookie")
        return mutableRequest
    }
    return request
};
imageView.sd_setImage(with: url, options: [], context: [.requestModifier] = requestModifier]) { }

@LilaQ
Copy link

LilaQ commented Mar 10, 2023

Thank you so much, this clears it up a lot for me! :)

@dreampiggy
Copy link
Collaborator

dreampiggy commented Mar 10, 2023

There may be a chance for SD to provide better convenient for common Swift users. And this repo can be merged into SDWebImage Core repo (current Objective-C only)

I've have a plan and demo in SDWebImage/swift fork branch to make SDWebImage 6.0 become Swift-Objc-mixed and provide massive better support like enum-associated-object/concurrency/result type, etc).

imageView.sd.setImage(with: url, options: [.header(["Foo": "Bar"])]) {
    switch $0 {
        case .success(let result):
            print(result.image.size)
            print(result.cacheType)
        case .failure(let error):
            if case let .badImageData(data, url) = error {
                 print("\(url) is not image url")
            } else {
                 print(error)
            }
    }
}
// or
do {
    let result = try await manager.loadImage(with: url)
} catch e {
    // ...
}

But seems has no extra time to make it finished recently...

Need someone from community to help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants