Custom request headers #324

Closed
mani1986 opened this Issue Dec 6, 2013 · 10 comments

Comments

8 participants
@mani1986

mani1986 commented Dec 6, 2013

Hi,

I can't see there is a way to have custom headers implemented with when getting an image by URL. Some API's require some authentication token for access to images.

If I were to implement this myself I guess adding this to UrlConnectionDownloader.java would be the way to go. Are there any plans for full implementation?

Mani

@dnkoutso

This comment has been minimized.

Show comment Hide comment
@dnkoutso

dnkoutso Dec 11, 2013

Collaborator

You can provide a custom Downloader for this. I dont see a problem here?

Collaborator

dnkoutso commented Dec 11, 2013

You can provide a custom Downloader for this. I dont see a problem here?

@antonj

This comment has been minimized.

Show comment Hide comment
@antonj

antonj Jan 21, 2014

To not implement alot of stuff that is already implemented, is this a possible way to use the existing OkHttpDownloader? I've tried it and it seems to work fine...

public static Picasso getImageLoader(Context ctx) {
    Picasso.Builder builder = new Picasso.Builder(ctx);
    builder.downloader(new OkHttpDownloader(ctx) {
        @Override
        protected HttpURLConnection openConnection(Uri uri) throws IOException {
            HttpURLConnection connection = super.openConnection(uri);
            connection.setRequestProperty("X-HEADER", "VAL");
            return connection;
        }
    });
    return builder.build();
}

Are there any potential bad side effects from extending and using the OkHttpDownloader this way?

antonj commented Jan 21, 2014

To not implement alot of stuff that is already implemented, is this a possible way to use the existing OkHttpDownloader? I've tried it and it seems to work fine...

public static Picasso getImageLoader(Context ctx) {
    Picasso.Builder builder = new Picasso.Builder(ctx);
    builder.downloader(new OkHttpDownloader(ctx) {
        @Override
        protected HttpURLConnection openConnection(Uri uri) throws IOException {
            HttpURLConnection connection = super.openConnection(uri);
            connection.setRequestProperty("X-HEADER", "VAL");
            return connection;
        }
    });
    return builder.build();
}

Are there any potential bad side effects from extending and using the OkHttpDownloader this way?

@JakeWharton

This comment has been minimized.

Show comment Hide comment
@JakeWharton

JakeWharton Jan 21, 2014

Collaborator

No. We designed it like that.


Jake Wharton
http://about.me/jakewharton

On Tue, Jan 21, 2014 at 7:59 AM, Anton Johansson
notifications@github.comwrote:

To not implement alot of stuff that is already implemented, is this a
possible way to use the existing OkHttpDownloader? I've tried it and it
seems to work fine...

public static Picasso getImageLoader(Context ctx) {
Picasso.Builder builder = new Picasso.Builder(ctx);
builder.downloader(new OkHttpDownloader(ctx) {
@override
protected HttpURLConnection openConnection(Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
connection.setRequestProperty("X-HEADER", "VAL");
return connection;
}
});
return builder.build();
}

Are there any potential bad side effects from extending and using the
OkHttpDownloader this way?


Reply to this email directly or view it on GitHubhttps://github.com/square/picasso/issues/324#issuecomment-32898622
.

Collaborator

JakeWharton commented Jan 21, 2014

No. We designed it like that.


Jake Wharton
http://about.me/jakewharton

On Tue, Jan 21, 2014 at 7:59 AM, Anton Johansson
notifications@github.comwrote:

To not implement alot of stuff that is already implemented, is this a
possible way to use the existing OkHttpDownloader? I've tried it and it
seems to work fine...

public static Picasso getImageLoader(Context ctx) {
Picasso.Builder builder = new Picasso.Builder(ctx);
builder.downloader(new OkHttpDownloader(ctx) {
@override
protected HttpURLConnection openConnection(Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
connection.setRequestProperty("X-HEADER", "VAL");
return connection;
}
});
return builder.build();
}

Are there any potential bad side effects from extending and using the
OkHttpDownloader this way?


Reply to this email directly or view it on GitHubhttps://github.com/square/picasso/issues/324#issuecomment-32898622
.

@dnkoutso

This comment has been minimized.

Show comment Hide comment
@dnkoutso

dnkoutso Jan 21, 2014

Collaborator

Closing this for now.

Collaborator

dnkoutso commented Jan 21, 2014

Closing this for now.

@dnkoutso dnkoutso closed this Jan 21, 2014

@vincekennedy

This comment has been minimized.

Show comment Hide comment
@vincekennedy

vincekennedy Jun 13, 2014

@antonj @JakeWharton I think I'm missing something with that solution. I'm trying to add a similar auth header to a request, but if I paste that code in to my project I end up with a NoClassDefFoundError for OkHttpClient. It's the latest, picasso-2.3.2.jar.

I'm also a little confused how it'd be used. getImageLoader(context) sets up the Picasso instance for the calling the load, into etc. Does the Picasso.load(uri) method pass that Uri down in to the OkHttpDowloader? I couldn't find it in the source.

@antonj @JakeWharton I think I'm missing something with that solution. I'm trying to add a similar auth header to a request, but if I paste that code in to my project I end up with a NoClassDefFoundError for OkHttpClient. It's the latest, picasso-2.3.2.jar.

I'm also a little confused how it'd be used. getImageLoader(context) sets up the Picasso instance for the calling the load, into etc. Does the Picasso.load(uri) method pass that Uri down in to the OkHttpDowloader? I couldn't find it in the source.

@rahulrj

This comment has been minimized.

Show comment Hide comment
@rahulrj

rahulrj Jul 27, 2014

@vincekennedy did you find any workaround for this. I am getting the same error.
@JakeWharton @dnkoutso posted here
http://stackoverflow.com/questions/24981469/custom-downloader-using-picasso

rahulrj commented Jul 27, 2014

@vincekennedy did you find any workaround for this. I am getting the same error.
@JakeWharton @dnkoutso posted here
http://stackoverflow.com/questions/24981469/custom-downloader-using-picasso

@ciskeboekelo

This comment has been minimized.

Show comment Hide comment
@ciskeboekelo

ciskeboekelo Mar 10, 2015

It seems OkHttpDownloader.openConnection() was removed in v2.5.0
What is the new way to add request headers to picasso requests?

EDIT: To answer my own question: see #900 and http://stackoverflow.com/questions/24273783/android-picasso-library-how-to-add-authentication-headers

It seems OkHttpDownloader.openConnection() was removed in v2.5.0
What is the new way to add request headers to picasso requests?

EDIT: To answer my own question: see #900 and http://stackoverflow.com/questions/24273783/android-picasso-library-how-to-add-authentication-headers

@JakeWharton

This comment has been minimized.

Show comment Hide comment
@JakeWharton

JakeWharton Mar 20, 2015

Collaborator

@ciskeboekelo Sorry for the delay here. OkHttp interceptors are the right thing to use here. You no longer have to subclass any of Picasso's classes!

Collaborator

JakeWharton commented Mar 20, 2015

@ciskeboekelo Sorry for the delay here. OkHttp interceptors are the right thing to use here. You no longer have to subclass any of Picasso's classes!

@pwightman

This comment has been minimized.

Show comment Hide comment
@pwightman

pwightman Apr 13, 2016

So to be clear, one must include OkHttp as a dependency if they want to add a header? No way to add a header otherwise? (I've been googling this for an hour trying to find relevant information, but it's been hard to come by!)

So to be clear, one must include OkHttp as a dependency if they want to add a header? No way to add a header otherwise? (I've been googling this for an hour trying to find relevant information, but it's been hard to come by!)

@JakeWharton

This comment has been minimized.

Show comment Hide comment
@JakeWharton

JakeWharton Apr 13, 2016

Collaborator

You can subclass UrlDownloader if you're not using OkHttp.

On Wed, Apr 13, 2016 at 1:56 PM Parker Wightman notifications@github.com
wrote:

So to be clear, one must include OkHttp as a dependency if they want to
add a header? No way to add a header otherwise? (I've been googling this
for an hour trying to find relevant information, but it's been hard to come
by!)


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#324 (comment)

Collaborator

JakeWharton commented Apr 13, 2016

You can subclass UrlDownloader if you're not using OkHttp.

On Wed, Apr 13, 2016 at 1:56 PM Parker Wightman notifications@github.com
wrote:

So to be clear, one must include OkHttp as a dependency if they want to
add a header? No way to add a header otherwise? (I've been googling this
for an hour trying to find relevant information, but it's been hard to come
by!)


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#324 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment