Best Fuel code snippet using com.github.kittinunf.fuel.core.requests.UploadRequest.dataParts
UploadBody.kt
Source:UploadBody.kt
...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 ->103 writeDataPart(stream, lazyDataPart(request)).toDouble()104 }105 // Sum and Trailer106 val writtenLength = 0L +107 parameterLength +108 filesWithHeadersLength +109 stream.writeBoundary() + stream.writeString("--") +110 stream.writeNewline()111 // This is a buffered stream, so flush what's remaining112 writtenLength.toLong().also { stream.flush() }113 }114 }115 /**116 * Returns the length of the body in bytes117 * @return [Long?] the length in bytes, null if it is unknown118 */119 override val length: Long? by lazy {120 (121 // Parameters size122 request.parameters.sumByDouble { (name, data) ->123 writeParameter(ByteArrayOutputStream(), name, data).toDouble()124 } +125 // Blobs / Files size126 request.dataParts.sumByDouble { lazyDataPart ->127 val dataPart = lazyDataPart(request)128 // Allow for unknown sizes129 val length = dataPart.contentLength ?: return@lazy null130 if (length == -1L) return@lazy -1L131 0.0 + writeDataPartHeader(ByteArrayOutputStream(), dataPart) + length + CRLF.size132 } +133 // Trailer size134 "--$boundary--".toByteArray(DEFAULT_CHARSET).size + CRLF.size135 ).toLong()136 }137 private val boundary: String by lazy {138 request[Headers.CONTENT_TYPE].lastOrNull()139 ?.let { Regex("boundary=([^\\s]+)").find(it)?.groupValues?.getOrNull(1)?.trim('"') }140 ?: throw BoundaryMissing(request)...
UploadRequest.kt
Source:UploadRequest.kt
...9import java.net.URL10import java.util.UUID11class UploadRequest private constructor(private val wrapped: Request) : Request by wrapped {12 override val request: UploadRequest = this13 val dataParts: MutableCollection<LazyDataPart> = mutableListOf()14 private fun ensureBoundary() {15 val contentType = this[Headers.CONTENT_TYPE].lastOrNull()16 // Overwrite the current content type17 if (contentType.isNullOrBlank() || !contentType.startsWith("multipart/form-data") || !Regex("boundary=[^\\s]+").containsMatchIn(contentType)) {18 this[Headers.CONTENT_TYPE] = "multipart/form-data; boundary=\"${UUID.randomUUID()}\""19 return20 }21 }22 override fun toString() = "Upload[\n\r\t$wrapped\n\r]"23 /**24 * Add one or multiple [dataParts] callbacks to be invoked to get a [DataPart] to write to the [UploadBody]25 * @param dataParts [LazyDataPart] the callbacks26 */27 fun add(vararg dataParts: LazyDataPart) = dataParts.fold(this, UploadRequest::plus)28 /**29 * Add one or multiple [DataPart]s to the [UploadBody]30 * @param dataParts [DataPart] the parts31 */32 fun add(vararg dataParts: DataPart) = plus(dataParts.toList())33 /**34 * Add a [DataPart] callback to be invoked to get a [DataPart] to write to the [UploadBody]35 * @param dataPart [LazyDataPart] the callback36 */37 fun add(dataPart: LazyDataPart) = plus(dataPart)38 /**39 * Add a [DataPart] to be written to the [UploadBody]40 * @param dataPart [DataPart] the part41 */42 fun add(dataPart: DataPart) = plus(dataPart)43 /**44 * Add a [DataPart] callback to be invoked to get a [DataPart] to write to the [UploadBody]45 * @param dataPart [LazyDataPart] the callback46 */47 operator fun plus(dataPart: LazyDataPart) = this.also { dataParts.add(dataPart) }48 /**49 * Add a [DataPart] to be written to the [UploadBody]50 * @param dataPart [DataPart] the part51 */52 operator fun plus(dataPart: DataPart) = plus { dataPart }53 /**54 * Add all [dataParts] to be written to the [UploadBody]55 * @param dataParts [Iterable<DataPart>] the iterable that yields [DataPart]s56 */57 operator fun plus(dataParts: Iterable<DataPart>) = dataParts.fold(this) { acc, dataPart ->58 acc.plus(dataPart)59 }60 /**61 * Add a [ProgressCallback] to the [requestProgress]62 * @param progress [ProgressCallback] the callback63 */64 fun progress(progress: ProgressCallback) = requestProgress(progress)65 companion object {66 val FEATURE: String = UploadRequest::class.java.canonicalName67 /**68 * Enable [UploadRequest] for the passed in [request]69 *70 * @note this sets the Content-Type to multipart/form-data; boundary=uuid even when there was already a71 * Content-Type set, unless it's multipart/form-data with a valid boundary.72 *73 * @param request [Request] the request to enable this for74 * @return [UploadRequest] the enhanced request75 */76 fun enableFor(request: Request) = request.enabledFeatures77 .getOrPut(FEATURE) {78 UploadRequest(request)79 .apply { this.body(UploadBody.from(this)) }80 .apply { this.ensureBoundary() }81 } as UploadRequest82 }83 @Deprecated("Use request.add({ BlobDataPart(...) }, { ... }, ...) instead", ReplaceWith(""), DeprecationLevel.ERROR)84 fun blobs(@Suppress("DEPRECATION") blobsCallback: (Request, URL) -> Iterable<Blob>): UploadRequest =85 throw NotImplementedError("request.blobs has been removed. Use request.add({ BlobDataPart(...) }, { ... }, ...) instead.")86 @Deprecated("Use request.add { BlobDataPart(...) } instead", ReplaceWith("add(blobsCallback)"))87 fun blob(@Suppress("DEPRECATION") blobCallback: (Request, URL) -> Blob): UploadRequest =88 throw NotImplementedError("request.blob has been removed. Use request.add { BlobDataPart(...) } instead.")89 @Deprecated("Use request.add({ ... }, { ... }, ...) instead", ReplaceWith(""), DeprecationLevel.ERROR)90 fun dataParts(dataPartsCallback: (Request, URL) -> Iterable<DataPart>): UploadRequest =91 throw NotImplementedError("request.dataParts has been removed. Use request.add { XXXDataPart(...) } instead.")92 @Deprecated("Use request.add({ FileDataPart(...) }, { ... }, ...) instead", ReplaceWith(""), DeprecationLevel.ERROR)93 fun sources(sourcesCallback: (Request, URL) -> Iterable<File>): UploadRequest =94 throw NotImplementedError("request.sources has been removed. Use request.add({ BlobDataPart(...) }, { ... }, ...) instead.")95 @Deprecated("Use request.add { FileDataPart(...)} instead", ReplaceWith("add(sourceCallback)"), DeprecationLevel.ERROR)96 fun source(sourceCallback: (Request, URL) -> File): UploadRequest =97 throw NotImplementedError("request.source has been removed. Use request.add { FileDataPart(...) } instead.")98 @Deprecated("Set the name via DataPart (FileDataPart, InlineDataPart, BlobDataPart) instead", ReplaceWith(""), DeprecationLevel.ERROR)99 fun name(nameCallback: () -> String): UploadRequest =100 throw NotImplementedError("request.name has been removed. Set the name via DataPart (FileDataPart, InlineDataPart, BlobDataPart) instead")101 @Deprecated("Set the name via DataPart (FileDataPart, InlineDataPart, BlobDataPart) instead", ReplaceWith(""), DeprecationLevel.ERROR)102 fun name(newName: String): UploadRequest =103 throw NotImplementedError("request.name has been removed. Set the name via DataPart (FileDataPart, InlineDataPart, BlobDataPart) instead")104}105fun Request.upload(): UploadRequest = UploadRequest.enableFor(this)...
dataParts
Using AI Code Generation
1import com.github.kittinunf.fuel.httpUpload2import com.github.kittinunf.fuel.core.requests.UploadRequest3import com.github.kittinunf.fuel.core.requests.uploadRequestOf4import com.github.kittinunf.fuel.core.requests.dataParts5import java.io.File6val file = File("C:\\Users\\username\\Pictures\\image.jpg")7 .httpUpload()8 .dataParts {9 append("key", "value")10 append("file", file)11 }12 .responseString()13import com.github.kittinunf.fuel.httpUpload14import com.github.kittinunf.fuel.core.requests.UploadRequest15import com.github.kittinunf.fuel.core.requests.uploadRequestOf16import com.github.kittinunf.fuel.core.requests.dataParts17import java.io.File18val file = File("C:\\Users\\username\\Pictures\\image.jpg")19 .httpUpload()20 .dataParts {21 append("key", "value")22 append("file", file)23 }24 .responseString()25import com.github.kittinunf.fuel.httpUpload26import com.github.kittinunf.fuel.core.requests.UploadRequest27import com.github.kittinunf.fuel.core.requests.uploadRequestOf28import com.github.kittinunf.fuel.core.requests.dataParts29import java.io.File30val file = File("C:\\Users\\username\\Pictures\\image.jpg")31val (
dataParts
Using AI Code Generation
1 listOf(2 "file" to File("some_file.txt").inputStream(),3 "file" to File("another_file.txt").inputStream()4}.responseString()5 listOf(6 "file" to File("some_file.txt").inputStream(),7 "file" to File("another_file.txt").inputStream()8}.responseString()9 listOf(10 "file" to File("some_file.txt").inputStream(),11 "file" to File("another_file.txt").inputStream()12}.responseString()13 listOf(14 "file" to File("some_file.txt").inputStream(),15 "file" to File("another_file.txt").inputStream()16}.responseString()17 listOf(18 "file" to File("some_file.txt").inputStream(),19 "file" to File("another_file.txt").inputStream()20}.responseString()21 listOf(22 "file" to File("some_file.txt").inputStream(),23 "file" to File("another_file.txt").inputStream()24}.responseString()
dataParts
Using AI Code Generation
1dataParts.forEach { multipart.add(it) }2multipart.response { _, _, result -> println(result) }3fileParts.forEach { multipart.add(it) }4multipart.response { _, _, result -> println(result) }5sourceParts.forEach { multipart.add(it) }6multipart.response { _, _, result -> println(result) }7multipart.body(body)8multipart.response { _, _, result -> println(result) }9multipart.body(body, contentType)10multipart.response { _, _, result -> println(result) }11multipart.body(body, contentType, contentLength)12multipart.response { _, _, result -> println(result) }13multipart.body(body, contentLength)14multipart.response { _, _, result -> println(result) }15multipart.body(body, contentType, contentLength, transferEncoding)16multipart.response { _, _, result -> println(result) }17multipart.body(body, contentType, contentLength, transferEncoding, disposition)18multipart.response { _, _, result -> println(result) }
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!