How to pass user credential to REST api?

I have coded a REST api which basically inserts, gets and updates the data from Elasticsearch. After implementing the basic security when I pass a curl command to the api providing credentials along with it (using -u), I get the following error:

[type=security_exception, reason=unable to authenticate user [a] for REST request]

Any idea how to pass the credentials to the REST api so that the same can pass it to Elasticsearch?
I am using version 6.8.16.

I don't understand what you did and how. Could you share some code?

@dadoonet The code is there in many files and is very long. So I don't think it will be feasible to share the code.
Basically the api builds the query from the curl command which is passed to it in some manner. After building the query it calls Elasticsearch with the built query.
I hope it helps in some way.

What code language u are using? it should come as additonal optional parameters when you pass the request

@theDor I am using JAVA. Should I pass it as a header? Will it work?

Are u using elasticsearch client or http generic client?