guys i'm trying to upload a file i understand it i...
# ktor
y
guys i'm trying to upload a file i understand it is done with submitFormWithBinaryData but how do i set the http method to PUT???
m
You can call HttpClient.put()
y
but i need to create the body
i managed to use submitFormWithBinaryData but i have a problem with the url, it’s a long url with token and i think the token is lost in the parsing
m
I do it like this
Copy code
suspend fun uploadFile(
        conversationId: Long,
        input: FileInput,
    ): Unit = resources.httpClient.put {
        conversationUrl(conversationId) {
            safePath("files")
        }

        val bytes = input.input.toByteArray()

        val data = formData {
            append("type", input.fileType.name)

            // The name header needs to be present for ktor to recognize it as a file
            append("file", bytes, headersOf(HttpHeaders.ContentDisposition, "filename=upload.file"))
        }

        body = MultiPartFormDataContent(data)
    }
you do not need to use submitFormWithBinary data to set a body
y
so you wrapped it with MultipartFormDataContent
i did not
m
It depends on what kind of request youre trying to send
y
i still get an error about the the request signature
i’m almost sure the query params gets lost
i think it gets stripped when i pass the the url to the put method
m
I don't know what request you're trying to send and how you're doing that
y
Copy code
client.put<HttpResponse>(writeUrl) {
    body =  MultiPartFormDataContent(formData {
        append("image", bitmapFile.readBytes(), Headers.build {
            append(HttpHeaders.ContentType, "image/png")
            append(HttpHeaders.ContentDisposition, "filename=${bitmapFile.name}.png")
        })
    })
m
that doesn't set headers
Headers.build just builds a Headers object but it doesn't set it
y
its being passed on…
this is my response: W/System.err: io.ktor.client.features.ClientRequestException: Client request(https://storage.googleapis.com/wtmimagerecognition-dev-01-vcm/-11/mobileReports/AmKm8VTSVurrYEc4pbNK/ok0j8z2le3lmb0r1p2f3kvih51qv7r.png?GoogleAccessId=wtmimagerecognition-dev-01%40appspot.gserviceaccount.com&amp;Expires=1621077215&amp;Signature=MyoSIgEDU7gergU3SlfiQoQBbB6QUCzNXEETV9K3Wb08mrUPC6hblogYXptDaAsvdZkX3MykLGJRmCbTa7jgKpQ9KIycgLkB7Rm2ds4AQdDGMl%2BqlMehBUgKJXZsCZkjKKpo1M%2BqODzWpx%2BB7SuPa6Wy38%2BekFQaEYH4jJ6Wvv%2F8Zbp2yBersBcwWw05adD3n%2BkG09QiHivGcmE0%2F%2F9x1vdihrYU2LnTMRylKHQltwAWYsjEP978K6L2etJ1rRGidZUeNZaqlhvU9xhbE5dn3goF8F8%2BdIF%2B%2FT9Ieagd%2BjsxmsrDvNQOddxnYkGSOx9X6bEma5kLOyzHFqJ9UwFqag==) invalid: 403 Forbidden. Text: “<?xml version=‘1.0’ encoding=‘UTF-8’?><Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Message><StringToSign>PUT multipart/form-data; boundary=4fde991a-39a8459b1540d23bdaf0ca595d2cb7-2d41ceae-41a60e90-75a56266351a 1621077215 /wtmimagerecognition-dev-01-vcm/-11/mobileReports/AmKm8VTSVurrYEc4pbNK/ok0j8z2le3lmb0r1p2f3kvih51qv7r.png</StringToSign></Error>”
m
I don't see the part where you set the request
y
just up above
i call put with these params
so i’m not sure how to move on from here
since i’ve set everything right
and my technique already worked in okhttp
same with okhttp that worked:
Copy code
val request = Request.Builder()
        .url(httpUrl)
        .put(bitmapFile.asRequestBody("image/png".toMediaType()))
        .build()