0

I am getting api response as String. eg:

HTTP/2 201 
server: nginx/1.14.0 (Ubuntu)
date: Fri, 23 Oct 2020 11:35:08 GMT
content-type: application/json
content-length: 51
set-cookie: userid=test; Path=/
set-cookie: uid=98-123-8h6; Path=/
set-cookie: session=hjdvcbdvbsdbuvibvbbubevbewuivbouewvjbivbivbweiubvuiebwviubewviubevwibewiuvbivebvbveb; Expires=Fri, 12-Dec-2020 11:45:08 GMT; HttpOnly; Path=/

{
  "message": "Login successful"
}

I need to convert this string to HttpEntity in java so that i can use entity.getBody to access the body and status code.

Vipin Gupta
  • 213
  • 3
  • 15
  • Try, string responsebody=Entityutils.toString(response.getEntity()) ; it will give you body of the response. – Nikita Bakshi Oct 23 '20 at 11:57
  • @NikitaBakshi I am can't use response.getEntity() because i have all the response in string. I need to convert this string response to HttpEntity to use it – Vipin Gupta Oct 23 '20 at 12:18
  • 1
    Why are you getting the entire raw HTTP message as a string? This looks like an [XY problem](https://meta.stackexchange.com/q/66377/351454), where you're trying to fix the symptom, instead of looking into fixing the problem. – Andreas Oct 23 '20 at 12:21
  • Are you referring to the [`HttpEntity`](https://docs.spring.io/spring-framework/docs/5.2.8.RELEASE/javadoc-api/org/springframework/http/HttpEntity.html) from the Spring Framework? If so, Spring should already be able to get you the API response as an `HttpEntity`, at least if you're using [`RestTemplate`](https://docs.spring.io/spring-framework/docs/5.2.8.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html), since a `ResponseEntity` *is* an `HttpEntity`. – Andreas Oct 23 '20 at 12:25

0 Answers0