Search code examples
javaspringhttp-headersresttemplatespring-rest

Adding Accept header in Spring


I'm trying to consume a JSON object from http://www.foaas.com/ in my Spring RESTful app. However they require content negotiation with an Accept header.

I have tried this method found here on SO:

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");

HttpEntity entity = new HttpEntity(headers);

HttpEntity response = restTemplate.exchange(foaasURI, HttpMethod.GET, entity, String.class);

I'm getting 403 forbidden at every attempt.

Using Postman for adding an Accept header works fine so I know everything is correct on foaas end. How do I proceed?


Solution

  • The problem is NOT with the "application/json" header. Your current code is successfully adding the "application/json" header.

    The server is rejecting your request because of some authentication issues.

    A 403 response generally indicates one of two conditions:

    Authentication was provided, but the authenticated user is not permitted to perform the requested operation.

    Please refer this link