0

I'm calling an API through java that returns an xml.

My code is:

JSONObject responses_obj = new JSONObject();
string url = "http://localhost:8080/myAPI";
URL obj = new URL(null, url , new sun.net.www.protocol.http.Handler());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

String urlParameters = "{\"patient_id\":" +"\"" +document_id_list[2] +"\", \"dob\" : " + "\"" + date_of_birth +  "\", \"document_id\" : " + "\""+ document_id + "\"}";

con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text");

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(
                                new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer responseJSON = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    responseJSON.append(inputLine);
    System.out.println(responseJSON);

    responses_obj.put("getDocument",responseJSON);
}
in.close()

When I'm printing the responseJSON I can see it as it is. However, when I'm viewing the content of responses_obj in browser I can see something like: <ClinicalDocument xmlns=\"urn:hl7-org:v3\" xmlns:epsos=\"....

The correct one should be <ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:epsos="...

Is ther a way to remove these "\" characters?

zinon
  • 4,427
  • 14
  • 70
  • 112
  • It looks like you're getting XML back, not JSON. I would validate what that stream looks like with Postman. It's unclear if the backslash is in the existing data or an artifact of your code. – stdunbar Jun 18 '19 at 23:46
  • Thank you @stdunbar. `Postman` also shows the backslashes. – zinon Jun 18 '19 at 23:59
  • Take a look at [this post](https://stackoverflow.com/questions/2242417/how-to-remove-the-backslash-in-string-using-regex-in-java/2242486). It's pretty easy to do. – stdunbar Jun 19 '19 at 00:08

0 Answers0