I have some code of ruby http post for hitting the API, but right now I need to convert it into java or groovy
this is my code on ruby
def loginWithEmailPassword(str_email, str_password)
uri = URI(url)
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
req['x-request-id'] = "xyz-#{SecureRandom.hex}"
req['user-agent'] = 'xyz'
req.body = {
email: str_email,
password: str_password
}.to_json
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https',
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
response = http.request(req) # Net::HTTPResponse object
if(response.code != '200')
puts response.body # Show response body
raise ("ERROR: login error... error code #{response.code}")
end
return response.body
end
end
This are my code on java
def loginApiWithEmailPassword(String sEmail, String sPassword){
URL url = new URL(m_url + "/login/password");
JSONObject json = new JSONObject();
json.put("email", sEmail);
json.put("password", sPassword);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// set header
conn.setRequestMethod("POST")
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("user-agent", aaa);
conn.setRequestProperty("x-request-id", getSecureRandom(s))
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
os.write(json.toJSONString().getBytes());
os.close();
// read the response
InputStream input = new BufferedInputStream(conn.getInputStream());
String result = org.apache.commons.io.IOUtils.toString(input, "UTF-8");
JSONObject jsonObject = new JSONObject(result);
input.close();
conn.disconnect();
return jsonObject;
}
I have tried to convert it into java but failed, stuck on error "javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"
And unable to continue to check the next function, may anyone able to help me to complete the http post for java or groovy