1

I have http response object "httpResponse" which is instance of class javax.ws.rs.core.Response. I have to convert it into a human readable string object for debugging. How can I achieve this?

final InputStream inputStream = (InputStream) httpResponse.getEntity();
final ByteArrayDataSource dataSource = new ByteArrayDataSource(inputStream, ContentType.MULTIPART_FORM_DATA.getMimeType());
final MimeMultipart multipartResponse = new MimeMultipart(dataSource);

I tried doing the below using the inputStream object. The output is all binary. Do I need to do base64 decoding? Thanks.

IOUtils.toString(inputStream, StandardCharsets.UTF_8))

Zack
  • 2,078
  • 10
  • 33
  • 58

1 Answers1

-1

As you said you need decoding.

// string to byte[]


byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8);



// byte[] to string



 String s = new String(bytes, StandardCharsets.UTF_8);

Source:

https://mkyong.com/java/how-do-convert-byte-array-to-string-in-java/

Here it is explained not only the conversion from bytes to text, but also the conversion from bytes to image or any kind of binary type.

LexFerrinson
  • 163
  • 7
  • I am already using standard charset : IOUtils.toString(inputStream, StandardCharsets.UTF_8)) – Zack May 03 '22 at 06:13
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 03 '22 at 11:27