Hello, i try convert this cURL in kotlin using ret...
# announcements
j
Hello, i try convert this cURL in kotlin using retrofit 2, but i cant and i really need this. Help me please. Code cURL-> curl -X POST \ URL_BASE\ -H 'Authorization: Bearer <your_acces_token>'\ -H 'content-type: application/json'\ -d '{ "process": "Attended", "rauthorityId": "<your_rauthorityId>" }'
s
Is ktor an option for you? val result: String = client.post { url("URL_BASE") header("Authorization", "Bearer $accessToken") body = TextContent( text = "{\"process\": \"Attended\",\"rauthorityId\": \"$rauthorityId\"}", contentType = ContentType.Application.Json ) }
j
Yep, is a really good option. Thank you!
s
glad i could help you. i switched from retrofit2 to ktor client a while ago in several projects and im really happy with it
n
that laos looks easy to do with fuel
just some snippet from my code:
Copy code
val requestSerializer: KSerializer<GraphQLRequest> = GraphQLRequest.serializer()
val resultSerializer: KSerializer<GraphQlResult> = GraphQlResult.serializer()
val (request, response, result) = <http://Fuel.post|Fuel.post>(url)
    .jsonBody(body = Json.stringify(requestSerializer, requestBody))
    .apply { headers.clear() }
    .header("User-Agent" to useragent, "Content-Type" to "application/json")
    .awaitObjectResponseResult(kotlinxDeserializerOf(loader = resultSerializer))
you can also do
<http://url.post|url.post>()
i do that for get requests
this is setting the jsonBody using a String , that takes care of the correct header technically, but i still clear it and make sure i set the headers exactly to what i want (i think there used to be some bug there that got fixed, i should validate if thats still necessary) this code also uses kotlinx-serialization for dealin with json but about every other lib is also available for it
691 Views