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

Nikola Milovic

08/12/2021, 11:40 AM
Using Ktor http client to make a request to my Nodejs backend. Tried sending a request manually and it is working, tried with my app, the body is missing. So it has to be something with my setup
Copy code
val data = JSONObject()
        data.put("email", "email")
        data.put("name", "testname")
        data.put("companyName", "testcompname")

        return singleFromCoroutine {
            try {
                val response: HttpResponse =
                    <http://client.post|client.post>("${serverUrl}${apiEndpoint}/account") {
                        body = data.toString()
                        contentType(ContentType.Application.Json)
                    }
Not sure what is happening, I'll post the entire request that gets received by the server
https://pastecode.io/s/1gidws0i Alongside
Copy code
[Node] SyntaxError: Unexpected token " in JSON at position 0
[Node]     at JSON.parse (<anonymous>)
a

Aleksei Tirman [JB]

08/12/2021, 11:55 AM
Could you please share your configuration for the client?
If you use the JsonFeature you should let the Ktor serialize the body. Here you find the explanation for a similar problem.
n

Nikola Milovic

08/12/2021, 12:16 PM
@Aleksei Tirman [JB] Seems that is the case, so basically I just drop the
data.toString
and leave it as a JSONObject?
a

Aleksei Tirman [JB]

08/12/2021, 12:22 PM
Yes.
2 Views