How to use guessContentType method of com.github.kittinunf.fuel.core.InlineDataPart class

Best Fuel code snippet using com.github.kittinunf.fuel.core.InlineDataPart.guessContentType

DataPart.kt

Source:DataPart.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core2import com.github.kittinunf.fuel.core.BlobDataPart.Companion.guessContentType3import com.github.kittinunf.fuel.core.FileDataPart.Companion.guessContentType4import java.io.File5import java.io.InputStream6import java.net.URLConnection7/**8 * Wrapper type that generates [DataPart] from a [Request]9 */10typealias LazyDataPart = (request: Request) -> DataPart11/**12 * Generic DataPart class13 *14 * @note based on https://tools.ietf.org/html/rfc757815 *16 * 4.2. Content-Disposition Header Field for Each Part17 *18 * Each part MUST contain a Content-Disposition header field [RFC2183]19 * where the disposition type is "form-data". The Content-Disposition20 * header field MUST also contain an additional parameter of "name"; the21 * value of the "name" parameter is the original field name from the22 * form (possibly encoded; see Section 5.1). For example, a part might23 * contain a header field such as the following, with the body of the24 * part containing the form data of the "user" field:25 *26 * Content-Disposition: form-data; name="user"27 *28 * 4.3. Multiple Files for One Form Field29 *30 * The form data for a form field might include multiple files.31 *32 * [RFC2388] suggested that multiple files for a single form field be33 * transmitted using a nested "multipart/mixed" part. This usage is34 * deprecated.35 *36 * 4.4. Content-Type Header Field for Each Part37 *38 * Each part MAY have an (optional) "Content-Type" header field, which39 * defaults to "text/plain". If the contents of a file are to be sent,40 * the file data SHOULD be labeled with an appropriate media type, if41 * known, or "application/octet-stream".42 */43sealed class DataPart {44 /**45 * Get the input stream that yields the contents of this [DataPart]46 * @return [InputStream] the input stream47 */48 abstract fun inputStream(): InputStream49 /**50 * The Content-Disposition header value51 */52 abstract val contentDisposition: String53 /**54 * The Content-Type header value55 */56 abstract val contentType: String57 /**58 * The content length or -1 or null if unknown59 */60 abstract val contentLength: Long?61 companion object {62 internal const val GENERIC_BYTE_CONTENT = "application/octet-stream"63 internal const val GENERIC_CONTENT = "text/plain"64 }65}66/**67 * Data part that has inline content68 *69 * @example Add metadata with a content-type70 *71 * val metadata = "{\"description\": \"test document from Kotlin \"}72 * request.add { InlineDataPart(metadata, contentType = "application/json", name = "metadata") }73 *74 * // ...boundary75 * // Content-Disposition: "form-data; name=metadata"76 * // Content-Type: "application/json"77 * //78 * // {"description": "test document from Kotlin"}79 *80 * @param content [String] the inline content81 * @param name [String] the field name82 * @param filename [String] the remote file name, or null83 * @param contentType [String] the content-type of the inline content; defaults to text/plain; charset=utf-884 * @param contentDisposition [String] defaults to form-data with name and filename set, unless filename is null85 */86data class InlineDataPart(87 val content: String,88 val name: String,89 val filename: String? = null,90 override val contentType: String = "$GENERIC_CONTENT; charset=utf-8",91 override val contentDisposition: String = "form-data; name=\"$name\"${if (filename != null) "; filename=\"$filename\"" else "" }"92) : DataPart() {93 override fun inputStream() = content.byteInputStream()94 override val contentLength get() = content.toByteArray(Charsets.UTF_8).size.toLong()95}96/**97 * DataPart that represents a file98 *99 * @example Add a file as a DataPart100 *101 * val file = File("foo.json")102 * request.add { FileDataPart(file, name = "field-name", filename = "remote-file-name.json") }103 *104 * // ...boundary105 * // Content-Disposition: "form-data; name=metadata; filename=remote-file-name.json"106 * // Content-Type: "application/json"107 * //108 * // <file-contents foo.json>109 *110 * @example Adding multiple files under the same field name, formerly using multipart/mixed:111 *112 * val file1 = File("foobar.json")113 * val file2 = File("baz.json")114 * request.add(FileDataPart(file1, name = "field-name"), FileDataPart(file2, name = "field-name"))115 *116 * // ...boundary117 * // Content-Disposition: "form-data; name=field-name; filename=foobar.json"118 * // Content-Type: "application/json"119 * //120 * // <file-contents foobar.json>121 * //122 * //123 * // ...boundary124 * // Content-Disposition: "form-data; name=field-name; filename=baz.json"125 * // Content-Type: "application/json"126 * //127 * // <file-contents baz.json>128 *129 * @example Adding multiple files as an array to a single field name:130 *131 * val file1 = File("foobar.json")132 * val file2 = File("baz.json")133 * request.add(FileDataPart(file1, name = "field-name[]"), FileDataPart(file2, name = "field-name[]"))134 *135 * // ...boundary136 * // Content-Disposition: "form-data; name=field-name[]; filename=foobar.json"137 * // Content-Type: "application/json"138 * //139 * // <file-contents foobar.json>140 * //141 * //142 * // ...boundary143 * // Content-Disposition: "form-data; name=field-name[]; filename=baz.json"144 * // Content-Type: "application/json"145 * //146 * // <file-contents baz.json>147 *148 * @param file [File] the source file149 * @param name [String] the field name, defaults to the file name without extension150 * @param filename [String] the remote file name, or null151 * @param contentType [String] the content type of the file contents; defaults to a guess using [guessContentType]152 * @param contentDisposition [String] defaults to form-data with name and filename set, unless filename is null153 */154data class FileDataPart(155 val file: File,156 val name: String = file.nameWithoutExtension,157 val filename: String? = file.name,158 override val contentType: String = FileDataPart.guessContentType(file),159 // For form data that represents the content of a file, a name for the160 // file SHOULD be supplied as well, by using a "filename" parameter of161 // the Content-Disposition header field. The file name isn't mandatory162 // for cases where the file name isn't available or is meaningless or163 // private; this might result, for example, when selection or drag-and-164 // drop is used or when the form data content is streamed directly from165 // a device.166 override val contentDisposition: String = "form-data; name=\"$name\"${if (filename != null) "; filename=\"$filename\"" else "" }"167) : DataPart() {168 override fun inputStream() = file.inputStream()169 override val contentLength get() = file.length()170 companion object {171 fun guessContentType(file: File) = try {172 URLConnection.guessContentTypeFromName(file.name)173 ?: BlobDataPart.guessContentType(file.inputStream())174 } catch (ex: NoClassDefFoundError) {175 // The MimetypesFileTypeMap class doesn't exists on old Android devices.176 GENERIC_BYTE_CONTENT177 }178 /**179 * Create a FileDataPart from a [path]180 *181 * @param path [File] the absolute path to the file182 * @param remoteFileName [String] the filename parameter for the DataPart, set to null to exclude, empty to use actual filename183 * @param name [String] the name for the field, uses [filename] without the extension by default184 * @param contentType [String] the Content-Type for the DataPart, set to null to [guessContentType]185 *186 * @return [FileDataPart] the DataPart187 */188 fun from(path: String, remoteFileName: String? = "", name: String? = null, contentType: String? = null): DataPart {189 val file = File(path)190 return FileDataPart(191 file = file,192 name = name ?: file.nameWithoutExtension,193 filename = remoteFileName?.let { if (it.isBlank()) file.name else it },194 contentType = contentType ?: guessContentType(file)195 )196 }197 /**198 * Create a FileDataPart from a [directory] and [filename]199 *200 * @param directory [File] the directory201 * @param filename [String] the filename relative to the [directory]202 * @param name [String] the name for the field, uses [filename] without the extension by default203 * @param remoteFileName [String] the filename parameter for the DataPart, set to null to exclude, empty to use [filename]204 * @param contentType [String] the Content-Type for the DataPart, set to null to [guessContentType]205 *206 * @return [FileDataPart] the DataPart207 */208 fun from(directory: String, filename: String, name: String? = null, remoteFileName: String? = "", contentType: String? = null): DataPart =209 // This happens if `from` is called with the path and the "remoteFileName" but without using named arguments210 if (File(directory).isFile && remoteFileName.isNullOrBlank())211 from(path = directory, remoteFileName = filename, name = name, contentType = contentType)212 else213 from(directory = File(directory), filename = filename, name = name, remoteFileName = remoteFileName, contentType = contentType)214 /**215 * Create a FileDataPart from a [directory] and [filename]216 *217 * @param directory [File] the directory218 * @param filename [String] the filename relative to the [directory]219 * @param name [String] the name for the field, uses [filename] without the extension by default220 * @param remoteFileName [String] the filename parameter for the DataPart, set to null to exclude, empty to use [filename]221 * @param contentType [String] the Content-Type for the DataPart, set to null to [guessContentType]222 *223 * @return [FileDataPart] the DataPart224 */225 fun from(directory: File?, filename: String, name: String? = null, remoteFileName: String? = "", contentType: String? = null): DataPart {226 val file = File(directory, filename)227 return FileDataPart(228 file = file,229 name = name ?: file.nameWithoutExtension,230 filename = remoteFileName?.let { if (it.isBlank()) filename else it },231 contentType = contentType ?: guessContentType(file)232 )233 }234 }235}236/**237 * DataPart from a generic InputStream238 *239 * @note not setting the content-length or setting it to -1L, results in the default Client using chunked encoding240 * with an arbitrary buffer size.241 *242 * @param inputStream [File] the source stream243 * @param name [String] the field name, required244 * @param filename [String] the remote file name, or null245 * @param contentLength [Long] the length in bytes, or -1 or null if it's not knows at creation time.246 * @param contentType [String] the content type of the file contents; defaults to a guess using [guessContentType]247 * @param contentDisposition [String] defaults to form-data with name and filename set, unless filename is null248 */249data class BlobDataPart(250 val inputStream: InputStream,251 val name: String,252 val filename: String? = null,253 override val contentLength: Long? = null,254 override val contentType: String = BlobDataPart.guessContentType(inputStream),255 override val contentDisposition: String = "form-data; name=\"$name\"${if (filename != null) "; filename=\"$filename\"" else "" }"256) : DataPart() {257 override fun inputStream() = inputStream258 companion object {259 fun guessContentType(stream: InputStream) = try {260 URLConnection.guessContentTypeFromStream(stream)261 ?: GENERIC_BYTE_CONTENT262 } catch (ex: NoClassDefFoundError) {263 // The MimetypesFileTypeMap class doesn't exists on old Android devices.264 GENERIC_BYTE_CONTENT265 }266 }267}...

Full Screen

Full Screen

guessContentType

Using AI Code Generation

copy

Full Screen

1val contentType = com.github.kittinunf.fuel.core.InlineDataPart.guessContentType(file)2val (_, _, result) = com.github.kittinunf.fuel.core.Fuel.upload("/path/to/endpoint").file(file, contentType)3val (data, error) = result.component1()4val (request, response, _) = data.component1()5val (url, statusCode, headers, _) = response.component1()6val (exception, response) = error.component1()7val (request, response, exception) = exception.component1()8val (url, statusCode, headers, _) = response.component1()9val (data, _) = response.component2()10val (request, response, _) = data.component1()11val (url, statusCode, headers, _) = response.component1()12val (_, data) = response.component2()13val (method, url, headers, _, _) = request.component1()14val (contentType, _) = headers.component1()

Full Screen

Full Screen

guessContentType

Using AI Code Generation

copy

Full Screen

1val file = File("path/to/file")2val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()3val file = File("path/to/file")4val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()5val file = File("path/to/file")6val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()7val file = File("path/to/file")8val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()9val file = File("path/to/file")10val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()11val file = File("path/to/file")12val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()13val file = File("path/to/file")14val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()15val file = File("path/to/file")16val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()

Full Screen

Full Screen

guessContentType

Using AI Code Generation

copy

Full Screen

1val contentType = InlineDataPart(ByteArray(0), "name").guessContentType()2val contentType = InputStreamPart(ByteArray(0).inputStream(), "name").guessContentType()3val contentType = FileDataPart(File(""), "name").guessContentType()4val contentType = FileInputPart(File("").inputStream(), "name").guessContentType()5val contentType = FileInputPart(File("").inputStream(), "name").guessContentType()6val contentType = FileInputPart(File("").inputStream(), "name").guessContentType()7val contentType = FileInputPart(File("").inputStream(), "name").guessContentType()8val contentType = FileInputPart(File("").inputStream(), "name").guessContentType()9val contentType = FileInputPart(File("").inputStream(), "name").guessContentType()10val contentType = FileInputPart(File("").inputStream(), "name").guessContentType()11val contentType = FileInputPart(File("").inputStream(), "name").guessContentType()

Full Screen

Full Screen

guessContentType

Using AI Code Generation

copy

Full Screen

1val extension = MimeTypeMap.getFileExtensionFromUrl(url)2val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)3val dataPart = InlineDataPart(url, mimeType, name)4val dataPart = FileDataPart(File(path), name)5val dataPart = FileDataPart(File(path), name, "text/plain")6val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8")7val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8", "filename.txt")8val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8", "filename.txt", "inline")9val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8", "filename.txt", "inline", "attachmentId")10val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8", "filename.txt", "inline", "attachmentId", "attachmentId")11val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8", "filename.txt", "inline", "attachmentId", "attachmentId", mapOf("key" to "value"))12val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8", "filename.txt", "inline", "attachmentId", "attachmentId", mapOf("key" to "value"), "customContentId")13val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8", "filename.txt", "inline", "attachmentId", "attachmentId", mapOf("key" to "value"), "customContentId", "customDisposition")14val dataPart = FileDataPart(File(path), name, "text/plain", "UTF-8", "filename.txt", "inline

Full Screen

Full Screen

guessContentType

Using AI Code Generation

copy

Full Screen

1val file = File("somefile.txt")2val dataPart = InlineDataPart(file.readBytes(), file.name, file.guessContentType())3val file = File("somefile.txt")4val dataPart = InlineDataPart(file.readBytes(), file.name, file.guessContentType())5val file = File("somefile.txt")6val dataPart = InlineDataPart(file.readBytes(), file.name, file.guessContentType())7val file = File("somefile.txt")8val dataPart = InlineDataPart(file.readBytes(), file.name, file.guessContentType())9val file = File("somefile.txt")10val dataPart = InlineDataPart(file.readBytes(), file.name, file.guessContentType())11val file = File("somefile.txt")12val dataPart = InlineDataPart(file.readBytes(), file.name, file.guessContentType())13val file = File("somefile.txt")14val dataPart = InlineDataPart(file.readBytes(), file.name, file.guessContentType())15val file = File("somefile.txt")16val dataPart = InlineDataPart(file.readBytes(), file.name, file.guessContentType())

Full Screen

Full Screen

guessContentType

Using AI Code Generation

copy

Full Screen

1 val contentType = com.github.kittinunf.fuel.core.InlineDataPart(file).guessContentType()2 val filePart = FileDataPart(file, name = "file", contentType = contentType)3 Fuel.upload("/upload")4 .source { request, url ->5 request.dataParts(listOf(filePart))6 }7 .responseObject { request, response, result ->8 println(request)9 println(response)10 println(result)11 }12 }13 }14}15Fuel.upload("/upload")16 .source { request, url ->17 request.dataParts(listOf(FileDataPart(file, name = "file")))18 }19 .responseObject { request, response, result ->20 println(request)21 println(response)22 println(result)23 }24Fuel.upload("/upload")25 .source { request, url ->26 val files = listOf(27 FileDataPart(file1, name = "file1"),28 FileDataPart(file2, name = "file2")29 request.dataParts(files)30 }31 .responseObject { request, response

Full Screen

Full Screen

guessContentType

Using AI Code Generation

copy

Full Screen

1val filePart = InlineDataPart(file, "file", contentType)2val (request, response, result) = Fuel.upload("/upload").source { request, url ->3request.body = listOf(filePart)4}5}6}7val filePart = InlineDataPart(file, "file", contentType)8val (request, response, result) = Fuel.upload("/upload").source { request, url ->9request.body = listOf(filePart)10}11}

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.

Most used method in InlineDataPart

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful