Title: JAVA - POST API CALL
Tags: java;api;http;post
Question: I'm trying to call an API with basic authorization but without success.
ClientConfig clientConfig = new ClientConfig();
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(&quot;username&quot;, &quot;password&quot;);
clientConfig.register( feature) ;
clientConfig.register(JacksonFeature.class);
Client client = ClientBuilder.newClient( clientConfig );
String URL = &quot;http://localhost:8080/teste/&quot; + taskId + &quot;/start&quot;;
WebTarget webTarget = client.target(URL);
Invocation.Builder invocationBuilder =  webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();
System.out.println(response.getStatus());
System.out.println(response.getStatusInfo());
if(response.getStatus() == 200)
System.out.println(response.getStatus());
I'm getting many errors like:
Could not find a suitable constructor in org.glassfish.jersey.message.internal.SourceProvider$SaxSourceReader class.
java.lang.IllegalArgumentException: Errors were discovered while reifying SystemDescriptor(
Can you help me please?
Some tips?
Greetings
Comment: Have you tested it with Postman? Just to double check that your backend works and it is not an issue with the code you just copied here.


Here is another answer: String URL = &quot;http://localhost:8080/teste/&quot; + taskId + &quot;/enable&quot;;
HttpPut request = new HttpPut(URL);
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(&quot;user&quot;, &quot;password&quot;)
);
try (CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build()) {
try (CloseableHttpResponse response = httpClient.execute(request)) {
// 401 if wrong user/password
System.out.println(response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
if (entity != null) {
// return it as a String
String result = EntityUtils.toString(entity);
System.out.println(result);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
I'm using this solution now and its work, but i need to send a raw body in this request, how i can do that?
Any tip?
Thanks
Comment for this answer: If you have another question, please ask a new question instead of posting it as answer

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:01/20/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><api><http><post>', 'Title': 'JAVA - POST API CALL', 'ViewCount': 27, 'id': 70785113, 'user_AboutMe': None, 'user_AccountId': 9289466.0, 'user_DisplayName': 'Nuno Santos', 'user_Reputation': 7.0, 'user_Views': 13.0, 'user_id': 7756552.0}}