How I can set my childname for post request for firebase realtime database. I use ktor 2.3.9 and I can make post request, but when I do it sent information break firebase Json, and i can't use data from this json file. ApiRoutes.SEARCH_USER is a link to my database. User is serializable data class. It contain login, firstname, lastname, patronymic and password.
This my first version post requstpost request.
suspend fun postUser(
id:Int,
login: String,
firstname: String,
lastname: String,
patronymic: String,
passwordNewUser: String
) {
client.post(ApiRoutes.SEARCH_USER) {
contentType(ContentType.Application.Json)
setBody(User(login, firstname, lastname, patronymic, passwordNewUser))
}
}
Before I find this versions, but this is version don't fix my problen, but this make stuctur, which I want.
suspend fun postUser(
id:Int,
login: String,
firstname: String,
lastname: String,
patronymic: String,
passwordNewUser: String
){
client.post(ApiRoutes.SEARCH_USER)
{
val file = User(login, lastname, firstname, patronymic, passwordNewUser)
contentType(ContentType.Application.Json)
var jsonchik = Json.encodeToJsonElement(mapOf("${id}" to file))
headers{
append(HttpHeaders.PublicKeyPins, "${id}")
}
setBody(jsonchik)
}
}
How I understand append can fix my problem, but I can't find needfull append
Since you're calling the REST API of the Firebase Realtime Database, a POST
request will always create a new child with an ID that is generated on the server.
If you want to specify the key yourself, you should use PUT
(instead of POST
). Here's an example of how to use PUT
based on the Firebase documentation:
curl -X PUT -d '{
"name": "Alan Turing",
"birthday": "June 23, 1912"
}' 'https://docs-examples.firebaseio.com/fireblog/users/alanisawesome.json'
To writes the alanisawesome
node under fireblog/users
.
The equivalent when using POST
would be:
curl -X POST -d '{
"name": "Alan Turing",
"birthday": "June 23, 1912"
}' 'https://docs-examples.firebaseio.com/fireblog/users.json'
But with POST
Firebase would generate a unique ID for the new child node on the server, while with PUT
we specify the example name (and complete path) of the node we want to write.