How to use setDoOutput method of com.github.kittinunf.fuel.toolbox.HttpClient class

Best Fuel code snippet using com.github.kittinunf.fuel.toolbox.HttpClient.setDoOutput

CustomHttpClient.kt

Source:CustomHttpClient.kt Github

copy

Full Screen

...62 instanceFollowRedirects = false63 for ((key, value) in request.headers) {64 setRequestProperty(key, value)65 }66 setDoOutput(connection, request.method)67 setBodyIfDoOutput(connection, request)68 }69 val contentEncoding = connection.contentEncoding ?: ""70 return Response(71 url = request.url,72 headers = connection.headerFields.filterKeys { it != null },73 contentLength = connection.contentLength.toLong(),74 statusCode = connection.responseCode,75 responseMessage = connection.responseMessage.orEmpty(),76 dataStream = try {77 val stream = connection.errorStream ?: connection.inputStream78 if (contentEncoding.compareTo("gzip", true) == 0) GZIPInputStream(stream) else stream79 } catch (exception: IOException) {80 connection.errorStream ?: connection.inputStream?.close()81 ByteArrayInputStream(ByteArray(0))82 }83 )84 } catch (exception: Exception) {85 throw FuelError(exception, ByteArray(0), Response(request.url))86 } finally {87 // As per Android documentation, a connection that is not explicitly disconnected88 // will be pooled and reused! So, don't close it as we need inputStream later!89 // connection.disconnect()90 }91 }92 private fun establishConnection(request: Request): URLConnection =93 if (proxy != null) request.url.openConnection(proxy) else request.url.openConnection()94 private fun setBodyIfDoOutput(connection: HttpURLConnection, request: Request) {95 val bodyCallback = request.bodyCallback96 if (bodyCallback != null && connection.doOutput) {97 val contentLength = bodyCallback(request, null, 0)98 if (request.type == Request.Type.UPLOAD)99 connection.setFixedLengthStreamingMode(contentLength.toInt())100 BufferedOutputStream(connection.outputStream).use {101 bodyCallback(request, it, contentLength)102 }103 }104 }105 private fun setDoOutput(connection: HttpURLConnection, method: Method) = when (method) {106 Method.GET, Method.DELETE, Method.HEAD -> connection.doOutput = false107 Method.POST, Method.PUT, Method.PATCH -> connection.doOutput = true108 }109 private fun allowMethods(vararg methods: String) {110 val methodsField = HttpURLConnection::class.java.getDeclaredField("methods").apply {111 isAccessible = true112 }113 Field::class.java.getDeclaredField("modifiers").apply {114 isAccessible = true115 setInt(methodsField, methodsField.modifiers and Modifier.FINAL.inv())116 }117 val newMethods =118 (methodsField.get(null) as? Array<*>)?.filterIsInstance<String>()?.toTypedArray()?.plus(methods)119 methodsField.set(null, newMethods)...

Full Screen

Full Screen

HttpClient.kt

Source:HttpClient.kt Github

copy

Full Screen

...31 for ((key, value) in request.httpHeaders) {32 setRequestProperty(key, value)33 }34 if (request.httpMethod == Method.PATCH) setRequestProperty("X-HTTP-Method-Override", "PATCH")35 setDoOutput(connection, request.httpMethod)36 setBodyIfDoOutput(connection, request)37 }38 return response.apply {39 httpResponseHeaders = connection.headerFields ?: emptyMap()40 httpContentLength = connection.contentLength.toLong()41 val contentEncoding = connection.contentEncoding ?: ""42 dataStream = try {43 val stream = connection.errorStream ?: connection.inputStream44 if (contentEncoding.compareTo("gzip", true) == 0) GZIPInputStream(stream) else stream45 } catch (exception: IOException) {46 try {47 connection.errorStream ?: connection.inputStream ?. close()48 } catch (exception: IOException) { }49 ByteArrayInputStream(kotlin.ByteArray(0))50 }51 //try - catch just in case both methods throw52 try {53 httpStatusCode = connection.responseCode54 httpResponseMessage = connection.responseMessage.orEmpty()55 } catch(exception: IOException) {56 throw exception57 }58 }59 } catch(exception: Exception) {60 throw FuelError().apply {61 this.exception = exception62 this.errorData = response.data63 this.response = response64 }65 } finally {66 //As per Android documentation, a connection that is not explicitly disconnected67 //will be pooled and reused! So, don't close it as we need inputStream later!68 //connection.disconnect()69 }70 }71 private fun establishConnection(request: Request): URLConnection {72 val urlConnection = if (proxy != null) request.url.openConnection(proxy) else request.url.openConnection()73 return if (request.url.protocol == "https") {74 val conn = urlConnection as HttpsURLConnection75 conn.apply {76 sslSocketFactory = request.socketFactory77 hostnameVerifier = request.hostnameVerifier78 }79 } else {80 urlConnection as HttpURLConnection81 }82 }83 private fun setBodyIfDoOutput(connection: HttpURLConnection, request: Request) {84 val bodyCallback = request.bodyCallback85 if (bodyCallback != null && connection.doOutput) {86 val contentLength = bodyCallback.invoke(request, null, 0)87 if (request.type == Request.Type.UPLOAD)88 connection.setFixedLengthStreamingMode(contentLength.toInt())89 val outStream = BufferedOutputStream(connection.outputStream)90 outStream.use {91 bodyCallback.invoke(request, outStream, contentLength)92 }93 }94 }95 private fun setDoOutput(connection: HttpURLConnection, method: Method) {96 when (method) {97 Method.GET, Method.DELETE, Method.HEAD -> connection.doOutput = false98 Method.POST, Method.PUT, Method.PATCH -> connection.doOutput = true99 }100 }101}...

Full Screen

Full Screen

setDoOutput

Using AI Code Generation

copy

Full Screen

1import com.github.kittinunf.fuel.core.FuelManager2import com.github.kittinunf.fuel.toolbox.HttpClient3import java.net.HttpURLConnection4val httpClient = HttpClient()5httpClient.apply {6 setDoOutput(true)7}8FuelManager.instance.apply {9 client = HttpClient().apply {10 setDoOutput(true)11 }12}13FuelManager.instance.apply {14 client = HttpClient().apply {

Full Screen

Full Screen

setDoOutput

Using AI Code Generation

copy

Full Screen

1 request.httpClient.apply {2 setDoOutput(true)3 setRequestProperty("Content-Type", "application/json")4 }5 request.body = "{\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1}"6 val (_, _, result) = request.responseString()7 println(result.get())

Full Screen

Full Screen

setDoOutput

Using AI Code Generation

copy

Full Screen

1HttpClient client = new HttpClient();2client.setDoOutput(true);3Request request = Fuel.post(url);4request.setBody(body);5Request request = Fuel.post(url);6request.setHeader(headers);7Request request = Fuel.post(url);8request.setResponseCallback(callback);9Request request = Fuel.post(url);10request.setProgressCallback(callback);11Request request = Fuel.post(url);12request.setProgressCallback(callback);13Request request = Fuel.post(url);14request.setReadTimeout(timeout);15Request request = Fuel.post(url);16request.setConnectTimeout(timeout);17Request request = Fuel.post(url);18request.setSocketTimeout(timeout);19Request request = Fuel.post(url);20request.setMethod(method);21Request request = Fuel.post(url);22request.setFollowRedirects(followRedirects);

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:

YouTube

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful