ImageRequest method type #985
-
Hi, Is there a way to change the method type from GET to POST for ImageRequest? Thanks, JP |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Not out of the box, though you can create an extension for this. Here's the code to do this (note: this only works in the 2.x alpha versions): private const val KEY = "http_method_type"
fun ImageRequest.Builder.httpMethod(method: String) = setParameter(KEY, method)
// Add this interceptor to the OkHttpClient that's used by your ImageLoader instance.
// https://coil-kt.github.io/coil/recipes/#using-a-custom-okhttpclient
class HttpMethodTypeInterceptor : Interceptor {
fun intercept(chain: Interceptor.Chain): Response {
val httpMethodType = chain.request.tag(Parameters::class.java)?.value(KEY)
return if (httpMethodType == null) {
chain.proceed(chain.request)
} else {
val newRequest = chain.request.newBuilder()
.method(httpMethodType, "".toRequestBody())
.build()
chain.proceed(newRequest)
}
}
}
// How to use:
imageView.load(url) {
httpMethod("POST")
} |
Beta Was this translation helpful? Give feedback.
-
Hi people, Two years later, I'm using Coil 2.4.0 and this discussion helped me a lot, I needed to POST data to create a QR-Code and show the result on screen. The interceptor code and extensions:data class Body(val content: String)
// Incomplete list of http methods, I'm only using Get/Post
sealed class HttpMethod {
object Post: HttpMethod()
object Get: HttpMethod()
}
fun ImageRequest.Builder.httpMethod(method: HttpMethod) = tag(HttpMethod::class.java, method)
fun ImageRequest.Builder.body(body: String) = tag(Body(content = body))
class HttpMethodTypeInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
// overriden body or the original one
val body =
originalRequest.tag(Body::class.java)?.content?.toRequestBody() ?:
originalRequest.body
val request = when(originalRequest.tag(HttpMethod::class.java)) {
HttpMethod.Get -> originalRequest.newBuilder().method("GET", body).build()
HttpMethod.Post -> originalRequest.newBuilder() .method("POST", body ?: "".toRequestBody()).build()
null -> originalRequest
}
return chain.proceed(request)
}
} In the AsyncImage Code model = ImageRequest.Builder(LocalContext.current)
.httpMethod(HttpMethod.Post)
.body("content=$qrContent")
.data("https://api.example.com/qr-code")
...
.build(),
... In the Application (As described in getting_started/image-loaders)class MyApplication : Application(), ImageLoaderFactory {
...
override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.okHttpClient{
OkHttpClient.Builder()
.addInterceptor(HttpMethodTypeInterceptor())
.build()
}
.build()
} It worked for me but if you see that I'm doing something stupid please let me know. Hope this help anybody :) |
Beta Was this translation helpful? Give feedback.
-
not working! |
Beta Was this translation helpful? Give feedback.
Not out of the box, though you can create an extension for this. Here's the code to do this (note: this only works in the 2.x alpha versions):