I suggest to use ready libraries to work with OpenAi API. There are few of them pretty good. Couple of examples: https://github.com/bay73/generic-ai/blob/main/README.md or https://github.com/aallam/openai-kotlin/blob/main/README.md