I have some Java code that has a String in the below format:
{username=username, password=password}
I want to convert it to JSON and pass it to a Http Post entity in an acceptable format. How do I do it?
Expected:
{
"username": "username",
"password": "password"
}
COde snippet:
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(
"https://url/api/v1/chk");
StringEntity input = new StringEntity(request.getBody().toString()); // I am sending an aplication/json content type JSON to the API that is invoking the https://url/api/v1/chk API input.setContentType("application/json");
postRequest.setEntity(input);HttpResponse response = httpClient.execute(postRequest); // This throws 400 bad request