Http 401 for the second request with java

I am trying to get completion responses for my queries, but only first request is getting succeed .Second request is resulting Http 401.
I am trying with java client.I got paid account.
Please help.
When I try with curl command with two different request(one after the other) both are getting proper response.

public class Test1 {
    public static void main(String[] args) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://api.openai.com/v1/completions");
        Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);

        String str="{\"model\":\"text-davinci-003\",\"prompt\":\"What is the capital of <country>\",\"temperature\":0.0,\"max_tokens\":150}";
         str=str.replace("<country>","Italy");
        String response = builder
                .header("Authorization", "Bearer sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .post(Entity.entity(str, MediaType.APPLICATION_JSON), String.class);
        System.out.println(response);

        str=str.replace("<country>","India");
          response = builder
                  .header("Authorization", "Bearer sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .post(Entity.entity(str, MediaType.APPLICATION_JSON), String.class);

        System.out.println(response);
        client.close();
    }
}```

REQUEST 1::::
{"id":"cmpl-6ZiuCeCszBpsHNIgfF1KJyCLwtq3J","object":"text_completion","created":1673972116,"model":"text-davinci-003","choices":[{"text":"\n\nRome is the capital of Italy.","index":0,"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":6,"completion_tokens":10,"total_tokens":16}}

REQUEST :2
Exception in thread "main" javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized

Issue is resolved. It is due to WebTarget creating invalid path second time.