0

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?

rram
  • 2,004
  • 5
  • 24
  • 37
  • `Accept=[text/plain, application/JSON, application/*+json, */*]` in request header means the client can accept any of these responses. Since the server can give responses in one of these formats it will not throw 406. Your server might be returning some other response or GraphQL might be throwing it due to some other scenario – Saheb May 09 '20 at 16:28
  • @Saheb - It's not my server. It is some other service I am just going to consume that. If I try the same request with Postman I'm getting results successfully with response header of `application/json`. My doubt is the third party server is refuse to give the response since there is a text/plain in the list. Of course there are other header accept options too but not sure why that third party returns 406. More over why spring boot is sending this header even when I remove it explicitly. Removing this(text/plain) might solve my issue. – rram May 10 '20 at 07:39
  • One idea to debug could be to carefully make a curl request out of the headers and other information printed in the debug logs from rest template and see which header/parameter might be causing the issue. – Saheb May 10 '20 at 14:02

0 Answers0