How to remove and add new Accept header to Spring boot Rest Template?

1.3k Views Asked by At

I'm consuming a GraphQL Service, it is returning 406 Not Acceptable if the Accept header is text/plain.

It is expecting to Accept as application/json So I tried override the RestTeamplate Headers. However it seems the Accept header test/plain is always present there. I confirmed this by enabling debug(logging.level.org.springframework.web.client.RestTemplate=DEBUG)

Console

 o.s.web.client.RestTemplate: Accept=[text/plain, application/json, application/*+json, */*]
 o.s.web.client.RestTemplate: Writing [{products(query: "title:tow*", first: 10) {edges {node {id legacyResourceId title}}}}] as "application/graphql"

Here is the code I tried to override the Accept header

HttpHeaders headers = new HttpHeaders();
RestTemplate restTemplate = new RestTemplate();

headers.add("Content-Type","application/graphql");
headers.setAccept(Collections.singletonList(new MediaType("application","json")));

String content = "{products(query: \"title:tow*\", first: 10) {edges {node {id title}}}}";
HttpEntity<String> requestEntity = new HttpEntity<String>(content, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

I even tried removing the Accept header first(headers.remove("Accept")) and then setAccept but still it doesn't remove it from the log.

Is there anything else need to be done?

Why is it not removing the text/plain from the Accept header?

Why I see multiple options in Accept when I set only one option?

0

There are 0 best solutions below