waltermcq
02/25/2020, 9:12 PMinstall(ContentNegotiation) {
gson {
setDateFormat(DateFormat.LONG)
setPrettyPrinting()
}
}
I created an extension function to catch post requests to an endpoint
fun Route.phrase(db: Repository) { //db param adheres to repository interface
post(PHRASE_ENDPOINT) {
// get request from call, then
val request = call.receive<Request>()
// add on db, then
val phrase = db.add(EmojiPhrase(request.emoji, request.phrase))
// respond to call, sending back emoji phrase that was added to repository
println("post: ${phrase.phrase} ${phrase.emoji}}")
call.respondText("post received", contentType = ContentType.Text.Plain)
}
}
I made a post request using curl
curl -X POST -H "Content-Type: application/json" -d tmp/request2.json <http://localhost:8080/api/v1/phrase>
this is the json file I am POSTing
{
"emoji": "1234",
"phrase": "test"
}
I get a server error when I try this:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $%
It looks like this error is coming from the
call.receive<Request>()call What am I doing wrong?
Alejandro Rios
02/25/2020, 9:26 PMwaltermcq
02/25/2020, 9:28 PMAlejandro Rios
02/25/2020, 9:47 PMcall.receiveParameters()
insteadwaltermcq
02/25/2020, 9:49 PMparameters
here mean what is in the query string? I'm trying to send json data, how would I do that with parameters..?Alejandro Rios
02/25/2020, 9:56 PMparameters["emoji"] parameters["phrase"]
waltermcq
02/25/2020, 9:56 PMAlejandro Rios
02/25/2020, 9:57 PMwaltermcq
02/25/2020, 10:14 PM{
but is getting "
Alejandro Rios
02/26/2020, 1:46 AMcurl
?