How to use URL.withParameters method of com.github.kittinunf.fuel.core.interceptors.ParameterEncoder class

Best Fuel code snippet using com.github.kittinunf.fuel.core.interceptors.ParameterEncoder.URL.withParameters


Source:ParameterEncoder.kt Github


Full Screen

1package com.github.kittinunf.fuel.core.interceptors2import com.github.kittinunf.fuel.core.FoldableRequestInterceptor3import com.github.kittinunf.fuel.core.Headers4import com.github.kittinunf.fuel.core.Method5import com.github.kittinunf.fuel.core.Parameters6import com.github.kittinunf.fuel.core.RequestTransformer7import ParameterEncoder : FoldableRequestInterceptor {10 override fun invoke(next: RequestTransformer): RequestTransformer {11 return inner@{ request ->12 val contentType = request[Headers.CONTENT_TYPE].lastOrNull()13 // Expect the parameters to be already encoded in the body14 if (contentType?.startsWith("multipart/form-data") == true) {15 return@inner next(request)16 }17 // If it can be added to the body18 if (request.body.isEmpty() && allowParametersInBody(request.method)) {19 if (contentType.isNullOrBlank() || contentType.startsWith("application/x-www-form-urlencoded")) {20 return@inner next(21 request22 .header(Headers.CONTENT_TYPE, "application/x-www-form-urlencoded")23 .body(encode(request.parameters))24 .apply { parameters = emptyList() }25 )26 }27 }28 // Has to be added to the URL29 next(30 request31 .apply { url = url.withParameters(parameters) }32 .apply { parameters = emptyList() }33 )34 }35 }36 private fun encode(parameters: Parameters) =37 parameters38 .filterNot { (_, values) -> values == null }39 .flatMap { (key, values) ->40 // Deal with arrays41 ((values as? Iterable<*>)?.toList() ?: (values as? Array<*>)?.toList())?.let {42 val encodedKey = "${URLEncoder.encode(key, "UTF-8")}[]"43 { value -> encodedKey to URLEncoder.encode(value.toString(), "UTF-8") }44 // Deal with regular45 } ?: listOf(URLEncoder.encode(key, "UTF-8") to URLEncoder.encode(values.toString(), "UTF-8"))46 }47 .joinToString("&") { (key, value) -> if (value.isBlank()) key else "$key=$value" }48 private fun allowParametersInBody(method: Method) = when (method) {49 Method.POST, Method.PATCH, Method.PUT -> true50 else -> false51 }52 private fun URL.withParameters(parameters: Parameters): URL {53 val encoded = ParameterEncoder.encode(parameters)54 if (encoded.isEmpty()) {55 return this56 }57 val joiner = if (toExternalForm().contains('?')) {58 // There is already some query59 if (query.isNotEmpty()) "&"60 // There is already a trailing ?61 else ""62 } else "?"63 return URL(toExternalForm() + joiner + encoded)64 }65}...

Full Screen

Full Screen


Using AI Code Generation


Full Screen

1val params = listOf("name" to "John Doe", "age" to 25)2val request = url.withParameters(params)3val response = request.httpGet().responseString()4println(response)5val params = listOf("name" to "John Doe", "age" to 25)6val request = url.withParameters(params)7val response = request.httpGet().responseString()8println(response)9val params = listOf("name" to "John Doe", "age" to 25)10val request = url.withParameters(params)11val response = request.httpGet().responseString()12println(response)13val params = listOf("name" to "John Doe", "age" to 25)14val request = url.withParameters(params)15val response = request.httpGet().responseString()16println(response)17val params = listOf("name" to "John Doe", "age" to 25)18val request = url.withParameters(params)19val response = request.httpGet().responseString()20println(response)21val params = listOf("name" to "John Doe", "age" to 25)22val request = url.withParameters(params)23val response = request.httpGet().responseString()24println(response)25val params = listOf("name" to "John Doe", "age" to 25)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Fuel automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in ParameterEncoder

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?