How to use writeTo method of com.github.kittinunf.fuel.core.Body class

Best Fuel code snippet using com.github.kittinunf.fuel.core.Body.writeTo

HttpClient.kt

Source:HttpClient.kt Github

copy

Full Screen

...236 ensureRequestActive(request, connection)237 }238 ).buffered(FuelManager.progressBufferSize)239 }240 body.writeTo(outputStream)241 connection.outputStream.flush()242 }243 private fun setDoOutput(connection: HttpURLConnection, method: Method) = when (method) {244 Method.GET, Method.HEAD, Method.OPTIONS, Method.TRACE -> connection.doOutput = false245 Method.DELETE, Method.POST, Method.PUT, Method.PATCH -> connection.doOutput = true246 }247 companion object {248 private val SUPPORTED_DECODING = listOf("gzip", "deflate; q=0.5")249 private fun coerceMethod(method: Method) = if (method == Method.PATCH) Method.POST else method250 }251}...

Full Screen

Full Screen

GoogleEmailCredential.kt

Source:GoogleEmailCredential.kt Github

copy

Full Screen

...188}189data class GoogleOauth2TokenResponse(val access_token: String, val refresh_token: String? = null)190data class GmailMessageSendRequest(val raw: String)191private fun MimeMessage.encodeToBase64() = ByteArrayOutputStream().use {192 writeTo(it)193 Base64.encodeToString(it.toByteArray(), Base64.NO_WRAP)194}195private val <T : Any> ResponseResultOf<T>.value: T196 get() {197 val (request, response, result) = this198 if (!response.isSuccessful) {199 error(200 "Request failed with ${response.statusCode}\n" +201 "----- REQUEST:\n" +202 request +203 "----- RESPONSE:\n" +204 response205 )206 }...

Full Screen

Full Screen

UploadBody.kt

Source:UploadBody.kt Github

copy

Full Screen

...29 */30 override fun asString(contentType: String?) = representationOfBytes("multipart/form-data")31 /**32 * Returns if the body is consumed.33 * @return [Boolean] if true, `writeTo`, `toStream` and `toByteArray` may throw34 */35 override fun isConsumed() = !inputAvailable36 /**37 * Returns the body emptiness.38 * @return [Boolean] if true, this body is empty39 */40 override fun isEmpty() = false41 /**42 * Returns the body as an [InputStream].43 *44 * @note callers are responsible for closing the returned stream.45 * @note implementations may choose to make the [Body] `isConsumed` and can not be written or read from again.46 *47 * @return the body as input stream48 */49 override fun toStream(): InputStream {50 throw UnsupportedOperationException(51 "Conversion `toStream` is not supported on UploadBody, because the source is not a single single stream." +52 "Use `toByteArray` to write the contents to memory or `writeTo` to write the contents to a stream."53 )54 }55 /**56 * Returns the body as a [ByteArray].57 *58 * @note Because the body needs to be read into memory anyway, implementations may choose to make the [Body]59 * readable once more after calling this method, with the original [InputStream] being closed (and release its60 * resources). This also means that if an implementation choose to keep it around, `isConsumed` returns false.61 *62 * @return the entire body63 */64 override fun toByteArray(): ByteArray {65 return ByteArrayOutputStream(length?.toInt() ?: 32)66 .use { stream ->67 writeTo(stream)68 stream.toByteArray()69 }70 .also { result ->71 // The entire body is now in memory, and can act as a regular body72 request.body(DefaultBody.from(73 { ByteArrayInputStream(result) },74 { result.size.toLong() }75 ))76 }77 }78 /**79 * Writes the body to the [OutputStream].80 *81 * @note callers are responses for closing the [OutputStream].82 * @note implementations may choose to make the [Body] `isConsumed` and can not be written or read from again.83 * @note implementations are recommended to buffer the output stream if they can't ensure bulk writing.84 *85 * @param outputStream [OutputStream] the stream to write to86 * @return [Long] the number of bytes written87 */88 override fun writeTo(outputStream: OutputStream): Long {89 if (!inputAvailable) {90 throw FuelError.wrap(IllegalStateException(91 "The inputs have already been written to an output stream and can not be consumed again."92 ))93 }94 inputAvailable = false95 val lazyDataparts = request.dataParts96 return outputStream.buffered().let { stream ->97 // Parameters98 val parameterLength = request.parameters.sumByDouble { (name, data) ->99 writeParameter(stream, name, data).toDouble()100 }101 // Blobs / Files102 val filesWithHeadersLength = lazyDataparts.sumByDouble { lazyDataPart ->...

Full Screen

Full Screen

DefaultBody.kt

Source:DefaultBody.kt Github

copy

Full Screen

...41 return ByteArray(0)42 }43 return ByteArrayOutputStream(length?.toInt() ?: 32)44 .use { stream ->45 writeTo(stream)46 stream.toByteArray()47 }48 .also { result ->49 openStream = { ByteArrayInputStream(result) }50 calculateLength = { result.size.toLong() }51 }52 }53 /**54 * Returns the body as an [InputStream].55 *56 * @note callers are responsible for closing the returned stream.57 * @note implementations may choose to make the [Body] `isConsumed` and can not be written or read from again.58 *59 * @return the body as input stream60 */61 override fun toStream(): InputStream = openStream().buffered().apply {62 // The caller is now responsible for this stream. This make sure that you can't call this twice without handling63 // it. The caller must still call `.close()` on the returned value when done.64 openStream = CONSUMED_STREAM65 }66 /**67 * Writes the body to the [OutputStream].68 *69 * @note callers are responses for closing the [OutputStream].70 * @note implementations may choose to make the [Body] `isConsumed` and can not be written or read from again.71 * @note implementations are recommended to buffer the output stream if they can't ensure bulk writing.72 *73 * @param outputStream [OutputStream] the stream to write to74 * @return [Long] the number of bytes written75 */76 override fun writeTo(outputStream: OutputStream): Long {77 val inputStream = openStream()78 // `copyTo` writes efficiently using a buffer. Reading ensured to be buffered by calling `.buffered`79 return inputStream.buffered()80 .use { it.copyTo(outputStream) }81 .also {82 // The outputStream could be buffered, but we are done reading, so it's time to flush what's left83 outputStream.flush()84 // This prevents implementations from consuming the input stream twice85 openStream = CONSUMED_STREAM86 }87 }88 /**89 * Returns the body emptiness.90 * @return [Boolean] if true, this body is empty91 */92 override fun isEmpty() = openStream === EMPTY_STREAM || (length == 0L)93 /**94 * Returns if the body is consumed.95 * @return [Boolean] if true, `writeTo`, `toStream` and `toByteArray` may throw96 */97 override fun isConsumed() = openStream === CONSUMED_STREAM98 /**99 * Returns the length of the body in bytes100 * @return [Long?] the length in bytes, null if it is unknown101 */102 override val length: Long? by lazy {103 calculateLength?.invoke()?.let {104 if (it == -1L) { null } else { it }105 }106 }107 companion object {108 private val EMPTY_STREAM = {109 ByteArrayInputStream(ByteArray(0))...

Full Screen

Full Screen

BodyTest.kt

Source:BodyTest.kt Github

copy

Full Screen

...22 @Test23 fun bodyIsConsumedAfterWriting() {24 val body = DefaultBody.from({ ByteArrayInputStream("body".toByteArray()) }, { 4 })25 assertThat(body.isConsumed(), equalTo(false))26 body.writeTo(ByteArrayOutputStream())27 assertThat(body.isConsumed(), equalTo(true))28 }29 @Test30 fun bodyFromString() {31 val value = "String Body ${Math.random()}"32 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))33 .body(value)34 .apply {35 val output = ByteArrayOutputStream(value.length)36 assertThat(body.length?.toInt(), equalTo(value.length))37 assertThat(body.toByteArray(), equalTo(value.toByteArray()))38 body.writeTo(output)39 assertThat(output.toString(), equalTo(value))40 }41 }42 @Test43 fun bodyFromByteArray() {44 val value = ByteArray(32).apply {45 for (i in 0..(this.size - 1)) {46 this[i] = ('A'..'z').random().toByte()47 }48 }49 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))50 .body(value)51 .apply {52 val output = ByteArrayOutputStream(value.size)53 assertThat(body.length?.toInt(), equalTo(value.size))54 assertArrayEquals(value, body.toByteArray())55 body.writeTo(output)56 assertArrayEquals(value, output.toByteArray())57 }58 }59 @Test60 fun bodyFromFile() {61 val value = "String Body ${Math.random()}"62 val file = File.createTempFile("BodyTest", ".txt")63 file.writeText(value)64 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))65 .body(file)66 .apply {67 val output = ByteArrayOutputStream(value.length)68 assertThat(body.length?.toInt(), equalTo(value.length))69 assertThat(body.toByteArray(), equalTo(value.toByteArray()))70 body.writeTo(output)71 assertThat(output.toString(), equalTo(value))72 }73 }74 @Test75 fun bodyFromStream() {76 val value = "String Body ${Math.random()}"77 val stream = ByteArrayInputStream(value.toByteArray())78 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))79 .body(stream)80 .apply {81 val output = ByteArrayOutputStream(value.length)82 assertThat(body.toByteArray(), equalTo(value.toByteArray()))83 body.writeTo(output)84 assertThat(output.toString(), equalTo(value))85 }86 }87 @Test(expected = FuelError::class)88 fun bodyFromCallbackCanOnlyBeReadOnce() {89 val body = DefaultBody.from({ ByteArrayInputStream("body".toByteArray()) }, { 4 })90 body.writeTo(ByteArrayOutputStream())91 body.writeTo(ByteArrayOutputStream())92 }93 @Test94 fun bodyToByteArrayLoadsItIntoMemory() {95 val value = "String Body ${Math.random()}"96 val body = DefaultBody.from({ ByteArrayInputStream(value.toByteArray()) }, { value.length.toLong() })97 body.toByteArray()98 val output = ByteArrayOutputStream(value.length)99 body.writeTo(output)100 assertThat(output.toString(), equalTo(value))101 }102 @Test103 fun requestWithBodyIsPrintableAfterConsumption() {104 val value = { ByteArrayInputStream("String Body ${Math.random()}".toByteArray()) }105 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))106 .body(value)107 .apply {108 val output = ByteArrayOutputStream()109 body.writeTo(output)110 assertThat(this.toString(), containsString("(consumed)"))111 }112 }113}...

Full Screen

Full Screen

RepeatableBody.kt

Source:RepeatableBody.kt Github

copy

Full Screen

...3import java.io.ByteArrayInputStream4import java.io.InputStream5import java.io.OutputStream6/**7 * A Repeatable Body wraps a body and on the first [writeTo] it keeps the bytes in memory so it can be written again.8 *9 * Delegation is not possible because the [body] is re-assigned, and the delegation would point to the initial10 * assignment.11 */12data class RepeatableBody(13 var body: Body14) : Body {15 /**16 * Writes the body to the [OutputStream].17 *18 * @note callers are responses for closing the [OutputStream].19 * @note implementations may choose to make the [Body] `isConsumed` and can not be written or read from again.20 * @note implementations are recommended to buffer the output stream if they can't ensure bulk writing.21 *22 * @param outputStream [OutputStream] the stream to write to23 * @return [Long] the number of bytes written24 */25 override fun writeTo(outputStream: OutputStream): Long {26 val repeatableBodyStream = ByteArrayInputStream(toByteArray())27 return body.writeTo(outputStream)28 .also { length -> body = DefaultBody.from({ repeatableBodyStream }, { length }) }29 }30 /**31 * Returns the body as a [ByteArray].32 *33 * @note Because the body needs to be read into memory anyway, implementations may choose to make the [Body]34 * readable once more after calling this method, with the original [InputStream] being closed (and release its35 * resources). This also means that if an implementation choose to keep it around, `isConsumed` returns false.36 *37 * @return the entire body38 */39 override fun toByteArray() = body.toByteArray()40 /**41 * Returns the body as an [InputStream].42 *43 * @note callers are responsible for closing the returned stream.44 * @note implementations may choose to make the [Body] `isConsumed` and can not be written or read from again.45 *46 * @return the body as input stream47 */48 override fun toStream() = body.toStream()49 /**50 * Returns the body emptiness.51 * @return [Boolean] if true, this body is empty52 */53 override fun isEmpty() = body.isEmpty()54 /**55 * Returns if the body is consumed.56 * @return [Boolean] if true, `writeTo`, `toStream` and `toByteArray` may throw57 */58 override fun isConsumed() = body.isConsumed()59 /**60 * Represents this body as a string61 * @param contentType [String] the type of the content in the body, or null if a guess is necessary62 * @return [String] the body as a string or a string that represents the body such as (empty) or (consumed)63 */64 override fun asString(contentType: String?) = body.asString(contentType)65 /**66 * Returns the length of the body in bytes67 * @return [Long?] the length in bytes, null if it is unknown68 */69 override val length = body.length70 /**...

Full Screen

Full Screen

Body.kt

Source:Body.kt Github

copy

Full Screen

...33 *34 * @param outputStream [OutputStream] the stream to write to35 * @return [Long] the number of bytes written36 */37 fun writeTo(outputStream: OutputStream): Long38 /**39 * Returns the body emptiness.40 * @return [Boolean] if true, this body is empty41 */42 fun isEmpty(): Boolean43 /**44 * Returns if the body is consumed.45 * @return [Boolean] if true, `writeTo`, `toStream` and `toByteArray` may throw46 */47 fun isConsumed(): Boolean48 /**49 * Returns the length of the body in bytes50 * @return [Long?] the length in bytes, null if it is unknown51 */52 val length: Long?53 /**54 * Makes the body repeatable by e.g. loading its contents into memory55 * @return [RepeatableBody] the body to be repeated56 */57 fun asRepeatable(): RepeatableBody = RepeatableBody(this)58 /**59 * Represents this body as a string...

Full Screen

Full Screen

RepeatableBodyTest.kt

Source:RepeatableBodyTest.kt Github

copy

Full Screen

...11 @Test12 fun repeatableBodyIsNeverConsumed() {13 val body = DefaultBody.from({ ByteArrayInputStream("body".toByteArray()) }, { 4 }).asRepeatable()14 assertThat(body.isConsumed(), equalTo(false))15 body.writeTo(ByteArrayOutputStream())16 assertThat(body.isConsumed(), equalTo(false))17 }18 @Test19 fun byteArrayBodyIsRepeatable() {20 val value = ByteArray(32).apply {21 for (i in 0..(this.size - 1)) {22 this[i] = ('A'..'z').random().toByte()23 }24 }25 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))26 .body(value)27 .apply {28 val output = ByteArrayOutputStream(value.size)29 assertThat(body.length?.toInt(), equalTo(value.size))30 assertThat(body.toByteArray(), equalTo(value))31 body.writeTo(output)32 assertThat(output.toString(), equalTo(String(value)))33 assertThat(body.isConsumed(), equalTo(false))34 }35 }36 @Test37 fun stringBodyIsRepeatable() {38 val value = "body"39 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))40 .body(value)41 .apply {42 val output = ByteArrayOutputStream(value.length)43 assertThat(body.length?.toInt(), equalTo(value.length))44 assertThat(body.toByteArray(), equalTo(value.toByteArray()))45 body.writeTo(output)46 assertThat(output.toString(), equalTo(value))47 assertThat(body.isConsumed(), equalTo(false))48 }49 }50 @Test51 fun requestWithRepeatableBodyIsPrintableAfterConsumption() {52 val value = "String Body ${Math.random()}"53 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))54 .body(value)55 .apply {56 val output = ByteArrayOutputStream()57 body.writeTo(output)58 assertThat(this.toString(), CoreMatchers.containsString(value))59 }60 }61}...

Full Screen

Full Screen

writeTo

Using AI Code Generation

copy

Full Screen

1val body = Body("hello world".toByteArray())2body.writeTo(object : OutputStream() {3override fun write(b: Int) {4print(b)5}6})7val body = Body("hello world".toByteArray())8body.writeTo(object : OutputStream() {9override fun write(b: Int) {10print(b)11}12})13val body = Body("hello world".toByteArray())14body.writeTo(object : OutputStream() {15override fun write(b: Int) {16print(b)17}18})19val body = Body("hello world".toByteArray())20body.writeTo(object : OutputStream() {21override fun write(b: Int) {22print(b)23}24})25val body = Body("hello world".toByteArray())26body.writeTo(object : OutputStream() {27override fun write(b: Int) {28print(b)29}30})31val body = Body("hello world".toByteArray())32body.writeTo(object : OutputStream() {33override fun write(b: Int) {34print(b)35}36})37val body = Body("hello world".toByteArray())38body.writeTo(object : OutputStream() {39override fun write(b: Int) {40print(b)41}42})43val body = Body("hello world".toByteArray())44body.writeTo(object : OutputStream() {45override fun write(b: Int) {46print(b)47}48})49val body = Body("hello world".toByteArray())50body.writeTo(object : OutputStream() {51override fun write(b: Int) {52print(b)53}54})55val body = Body("hello world".toByteArray())56body.writeTo(object : OutputStream() {57override fun write(b: Int) {58print(b)

Full Screen

Full Screen

writeTo

Using AI Code Generation

copy

Full Screen

1 val (request, response, result) = Fuel.upload("/upload")2 .source { request, url ->3 val body = Body()4 body.writeTo(request.outputStream())5 request.outputStream().close()6 }7 .response()8 val (request, response, result) = Fuel.upload("/upload")9 .source { request, url ->10 val body = Body()11 body.writeTo(request.outputStream())12 request.outputStream().close()13 }14 .response()15 val (request, response, result) = Fuel.upload("/upload")16 .source { request, url ->17 val body = Body()18 body.writeTo(request.outputStream())19 request.outputStream().close()20 }21 .response()22 val (request, response, result) = Fuel.upload("/upload")23 .source { request, url ->24 val body = Body()25 body.writeTo(request.outputStream())26 request.outputStream().close()27 }28 .response()29 val (request, response, result) = Fuel.upload("/upload")30 .source { request, url ->31 val body = Body()32 body.writeTo(request.outputStream())33 request.outputStream().close()34 }35 .response()36 val (request, response, result) = Fuel.upload("/upload")37 .source { request, url ->38 val body = Body()39 body.writeTo(request.outputStream())40 request.outputStream().close()41 }42 .response()43 val (request, response, result) = Fuel.upload("/upload")44 .source { request, url ->45 val body = Body()46 body.writeTo(request.outputStream())47 request.outputStream().close()48 }49 .response()

Full Screen

Full Screen

writeTo

Using AI Code Generation

copy

Full Screen

1{2 "args": {},3 "files": {},4 "form": {},5 "headers": {6 },7}8{9 "args": {},10 "files": {

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