https://kotlinlang.org logo
#ktor
Title
# ktor
p

PoisonedYouth

08/07/2022, 11:57 AM
Hey guys, does Ktor support deserialization of form data to object (when using together with Thymeleaf) out of the box or is it necessary to write custom mapper? I installed content negotiation with Jackson but always get an exception (io.ktor.request.CannotTransformToTypeException).
a

Aleksei Tirman [JB]

08/08/2022, 11:43 AM
Could you please describe in more detail what do you want to deserialize?
p

PoisonedYouth

08/08/2022, 1:19 PM
I have an html form:
Copy code
<form th:action="@{new}" th:object="${customer}" method="post">
        <div class="mb-3">
            <label for="firstName" class="form-label">Firstname</label>
            <input name="firstName" class="form-control" type="text" id="firstName" placeholder="Firstname"
                   th:value="*{firstName}">
        </div>
        <div class="mb-3">
            <label for="lastName" class="form-label">Lastname</label>
            <input name="lastName" class="form-control" type="text" id="lastName" placeholder="Firstname"
                   th:value="*{lastName}">
        </div>
        <div class="mb-3">
            <label for="birthdate" class="form-label">Birthdate</label>
            <input name="birthdate" class="form-control" type="date" id="birthdate" placeholder="Birthdate"
                   th:value="*{birthdate}">
        </div>
        <div class="mb-3">
            <label for="street" class="form-label">Street</label>
            <input name="street" class="form-control" type="text" id="street" placeholder="Street" th:value="*{street}">
        </div>
        <div class="mb-3">
            <label for="streetNumber" class="form-label">Street Number</label>
            <input name="streetNumber" class="form-control" type="text" id="streetNumber" placeholder="Street Number"
                   th:value="*{streetNumber}">
        </div>
        <div class="mb-3">
            <label for="zipCode" class="form-label">Zip Code</label>
            <input name="zipCode" class="form-control" type="text" id="zipCode" placeholder="Zip Code"
                   th:value="*{zipCode}">
        </div>
        <div class="mb-3">
            <label for="city" class="form-label">City</label>
            <input name="city" class="form-control" type="text" id="city" placeholder="City" th:value="*{city}">
        </div>

        <button type="submit" class="btn btn-primary">Save</button>
    </form>
which is producing the following form data when calling
Copy code
call.receiveText()
Copy code
firstName=John&lastName=Doe&birthdate=2000-08-09&street=Main+Streete&streetNumber=12+A&zipCode=90000&city=Los+Angeles
Using
Copy code
call.receive<CustomerData>()
is throwing "io.ktor.server.plugins.CannotTransformContentToTypeException: Cannot transform this request's content to com.poisonedyouth.CustomerData" (bearbeitet)
a

Aleksei Tirman [JB]

08/08/2022, 5:26 PM
Unfortunately, Ktor doesn’t provide a serializer for form-data parameters. You can either write your own serializer or receive a Parameters object and convert it to
CustomerData
.
p

PoisonedYouth

08/08/2022, 5:31 PM
Thanks for this information @Aleksei Tirman [JB], I already implemented your second suggestion, but wondering that no solution exist for this.
5 Views