I am trying to consume a Rest API using Groovy and here it is the code I am using :
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
CloseableHttpClient client = HttpClients.createDefault();
HttpResponse response = null;
HttpPost request = new HttpPost("http://path_to_my_server/rest_api_path");
String auth = "login:password";
String encoding = auth.bytes.encodeBase64().toString()
request.setHeader("Authorization", "Basic " + encoding);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("postVar", new StringBody("value",ContentType.MULTIPART_FORM_DATA));
HttpEntity entity = builder.build();
request.setEntity(entity);
response = client.execute(request);
the problem is that every time I execute it, it shows me an error :
error javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
I did some reaserch and I tested some Codes like :
https://gist.github.com/barata0/63705c0bcdd1054af2405e90c06f6b71
https://github.com/jgritman/httpbuilder/wiki/SSL
How to use SSL with a self-signed certificate in groovy?
All of those did not work , any help please?