0

I have a class that implements Route interface (package spark) that is supposed to return a String in the handle function. When I convert the response to String I have two extra " in the beginning and the end of my String and the \n is not recognized as 1 char in my String but rather as 2. Does anyone know the fix?

This is the code and response is an instance of HttpResponse:

Assert.assertEquals(200, response.getStatusLine().getStatusCode());
String output = EntityUtils.toString(response.getEntity())

The next Assertion fails

Assert.assertEquals(10, output.length());
Java.lang.AssertionError: 
Expected :10
Actual   :13

And according to System.out output is

"123456789\n"

While it should be 123456789 and a \n at the end of it but I don't believe \n should be shown in System.out.print()

user10938362
  • 3,991
  • 2
  • 12
  • 29
Peggy
  • 394
  • 6
  • 22

2 Answers2

0

This may help,

HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);

Reference from - How can I get an http response body as a string in Java?

suketup
  • 469
  • 7
  • 12
0

So this is what solved it for me at the end, if anyone else has the same problem:

String output = EntityUtils.toString(response.getEntity())
String content = new Gson().fromJson(output, String.class);
Peggy
  • 394
  • 6
  • 22