Maybe you can benefit from using ready libraries and not do all http stuff yourself? For example look into https://github.com/bay73/generic-ai/blob/main/README.md or https://github.com/aallam/openai-kotlin/blob/main/README.md