0

I am trying a hit a POST API which is returning a token of the format - "ABC:XYZ". When I am trying to parse this into a String it is not able to do it. I suspect, it is because of the presence of a colon( : ) in the token because of which it is treating it as a JSON. I am using REST Template. I tried setting the Accept Header as MediaType.TEXT_PLAIN but that also didn't work for me.

Below is my code

        ResponseEntity<Object> response = null;
        HttpHeaders headers = new HttpHeaders();
        try {
            UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl("https://XXXX/YYYY");
            Map<String, String> criteria = new HashMap<>();
            criteria.put("username", "JOHNDOE"); 
            uriBuilder.queryParam("username", "{username}");
            criteria.put("target_site", "SITE");
            uriBuilder.queryParam("target_site", "{target_site}");

            headers.setAccept(Collections.singletonList(MediaType.TEXT_PLAIN));
            HttpEntity<String> entity = new HttpEntity<>(headers);

           // ParameterizedTypeReference<String> responseType = new ParameterizedTypeReference<String>() {
           // };
            response =
                    restTemplateSSLIgnore.exchange(
                            uriBuilder.build().toUriString(),
                            HttpMethod.POST,
                            entity,
                            Object.class,
                            criteria);
            logger.info(response.getBody());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return false;
        }
        return response.getBody();

Can somebody help me with this?

Nayan Arora
  • 47
  • 1
  • 7
  • First of all, don't dump an error message in the title. It's very hard to read and writing a more informative title might help you get more people reading your question. Secondly could you add a json sample and your failing parsing code so we can better understand what is going on. – Joakim Danielson Mar 12 '19 at 10:11
  • Thanks, Joakim. I hope it's more clear now. – Nayan Arora Mar 12 '19 at 13:56

0 Answers0