travel: send POST bodies as JSON
- Commit
2d6d692eed25b78467d046f0d46b3260805584ab- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-01 17:42 UTC (1 month ago)
- Parents
de7db67
The shared network client sends non-String bodies via toString(), so trip creation, ancillary selection and stay quote/booking were posting "TripRequestDto(...)" and the server rejected them as unparseable JSON (shown as "Something went wrong"). Encode request DTOs with encodeDefaults (the server requires fields like title/email even when empty) and omit nulls.
Diff
travel/src/main/java/com/vayunmathur/travel/network/StaysApi.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/network/StaysApi.kt b/travel/src/main/java/com/vayunmathur/travel/network/StaysApi.ktindex 7654ef8b64..4b7fde796e 100644--- a/travel/src/main/java/com/vayunmathur/travel/network/StaysApi.kt+++ b/travel/src/main/java/com/vayunmathur/travel/network/StaysApi.kt@@ -66,12 +66,12 @@ object StaysApi { private const val MAX_ERROR_SNIPPET_LENGTH = 300 - private suspend inline fun <reified T> postJson(url: String, body: Any, fallback: String): T {+ private suspend inline fun <reified B, reified T> postJson(url: String, body: B, fallback: String): T { val res = NetworkClient.performRequest( url = url, method = "POST", headers = mapOf("Content-Type" to "application/json"),- body = if (body is String && body.isEmpty()) null else body,+ body = travelRequestBody(body), ) if (!res.isSuccess) throw travelHttpError(res.status, res.body, fallback) return json.decodeFromString(res.body)
travel/src/main/java/com/vayunmathur/travel/network/TravelApiCore.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/network/TravelApiCore.kt b/travel/src/main/java/com/vayunmathur/travel/network/TravelApiCore.ktindex 46ac510305..64d77a3fa9 100644--- a/travel/src/main/java/com/vayunmathur/travel/network/TravelApiCore.kt+++ b/travel/src/main/java/com/vayunmathur/travel/network/TravelApiCore.kt@@ -15,6 +15,23 @@ internal const val MAX_ERROR_SNIPPET_LENGTH = 300 internal val travelJson = Json { ignoreUnknownKeys = true } +/**+ * Request bodies: every field is sent (the server requires e.g. `title` and+ * `email` even when empty), nulls are omitted.+ */+internal val travelRequestJson = Json {+ encodeDefaults = true+ explicitNulls = false+}++/**+ * JSON text for a POST body; null for "no body" (an empty string). The shared+ * network client sends non-String bodies via `toString()`, so DTOs must be+ * encoded here.+ */+internal inline fun <reified B> travelRequestBody(body: B): String? =+ if (body is String) body.ifEmpty { null } else travelRequestJson.encodeToString(body)+ internal fun travelEnc(s: String): String = URLEncoder.encode(s, "UTF-8") /** Fallback text when an error body carries no usable message. */@@ -85,12 +102,12 @@ internal suspend inline fun <reified T> travelGetJson(url: String, fallback: Str } /** POST [body] to [url] and decode the JSON result, surfacing server errors. */-internal suspend inline fun <reified T> travelPostJson(url: String, body: Any, fallback: String): T {+internal suspend inline fun <reified B, reified T> travelPostJson(url: String, body: B, fallback: String): T { val res = NetworkClient.performRequest( url = url, method = "POST", headers = mapOf("Content-Type" to "application/json"),- body = if (body is String && body.isEmpty()) null else body,+ body = travelRequestBody(body), ) if (!res.isSuccess) throw travelHttpError(res.status, res.body, fallback) return travelJson.decodeFromString(res.body)
travel/src/test/java/com/vayunmathur/travel/network/RequestBodyTest.kt
diff --git a/travel/src/test/java/com/vayunmathur/travel/network/RequestBodyTest.kt b/travel/src/test/java/com/vayunmathur/travel/network/RequestBodyTest.ktnew file mode 100644index 0000000000..aece000d9e--- /dev/null+++ b/travel/src/test/java/com/vayunmathur/travel/network/RequestBodyTest.kt@@ -0,0 +1,38 @@+package com.vayunmathur.travel.network++import kotlinx.serialization.json.Json+import kotlinx.serialization.json.jsonArray+import kotlinx.serialization.json.jsonObject+import kotlinx.serialization.json.jsonPrimitive+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNull++/** POST bodies must be real JSON (the network client would otherwise send `toString()`). */+class RequestBodyTest {+ @Test+ fun tripRequestEncodesAsJsonWithDefaults() {+ val body = travelRequestBody(+ TripRequestDto(+ offerIds = listOf("o1"),+ passengers = listOf(PassengerInputDto(id = "pax_1", givenName = "Ann", familyName = "Doe")),+ contact = TripContactDto(email = "[email protected]", phoneNumber = "+14155550123"),+ )+ )!!+ val json = Json.parseToJsonElement(body).jsonObject+ assertEquals("o1", json["offerIds"]!!.jsonArray[0].jsonPrimitive.content)+ val pax = json["passengers"]!!.jsonArray[0].jsonObject+ assertEquals("Ann", pax["givenName"]!!.jsonPrimitive.content)+ // Empty defaults the server requires are still present…+ assertEquals("", pax["title"]!!.jsonPrimitive.content)+ assertEquals("", pax["bornOn"]!!.jsonPrimitive.content)+ // …and nulls are omitted.+ assertFalse("identityDocument" in pax)+ }++ @Test+ fun emptyStringMeansNoBody() {+ assertNull(travelRequestBody(""))+ }+}