8

I'm implementing custom response headers on a resource, after reading over a similar question, I added an 'Access-Control-Expose-Headers' to my CORS headers, but I must not have something configured correctly.

Here are the headers I receive on a GET request of the resource: I receive a 'Response-Header' of "Steve", which should be allowed by the 'Access-Control-Expose-Headers'

Access-Control-Allow-Headers:x-requested-with, Request-Header, Response-Header
Access-Control-Allow-Methods:POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:Response-Header
Response-Header:Steve

But, Angular is still does not have access to that custom header. I get back my custom Request-Header, but not my custom Response-Header:

{
  "data": {
    "id": 2,
    "content": "Hello, frank!"
  },
  "status": 200,
  "config": {
    "method": "GET",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "url": "http:\/\/localhost:8080\/greeting",
    "headers": {
      "Accept": "application\/json, text\/plain, *\/*",
      "Request-Header": "frank"
    }
  },
  "statusText": "OK"
}

Angular 1.3 $httpProvider.interceptor:

'use strict';
angular
  .module('headerDemoUiApp')
  .factory('AuthInterceptor', function AuthInterceptor(nameService) {

    return {
      request: handleRequest,
      response: handleResponse
    };

    function handleRequest(config) {
      if (angular.isDefined(config.headers)) {
        config.headers['Request-Header'] = "frank";
      }
      return config;
    }

    function handleResponse(response) {
      console.log('Response: ' + JSON.stringify(response));

      if (angular.isDefined(response.config.headers['Response-Header'])) {
        var respHead = response.config.headers['Response-Header'];
        console.log('Response-Header: ' + respHead);
        nameService.responsename=respHead;
      }
      return response;
    }

  });

And, the Spring version 4.1.6 @RestController adding the header.

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(
            @RequestParam(value = "name", defaultValue = "World") String name,
            HttpServletResponse response,
            @RequestHeader("Request-Header") String headerName) {

        // Sets our custom response header
        response.setHeader("Response-Header","Steve");

        return new Greeting(counter.incrementAndGet(),
                String.format(template, headerName));
    }
}

API Code Repo

UI Code Repo

Thanks for taking a look!

2 Answers 2

3

Issue is with your API GreetingController.java

change the greeting method to return ResponseEntity and use HttpHeaders, as below

@RequestMapping("/greeting")
    public ResponseEntity<Greeting> greeting(@RequestParam(value = "name", defaultValue = "World") String name, @RequestHeader("Request-Header") String headerName) {

        MultiValueMap<String, String> headers = new HttpHeaders();
        // Sets our custom response header
        headers.add("Response-Header","Steve");
        return new ResponseEntity<Greeting>(new Greeting(counter.incrementAndGet(),String.format(template, headerName)), headers, HttpStatus.OK); 
    }

Below was my headers response with Postman client

Content-Length → 2
Content-Type → text/plain;charset=UTF-8
Date → Wed, 27 May 2015 15:53:47 GMT
Response-Header → Steve
Server → Apache-Coyote/1.1
Sign up to request clarification or add additional context in comments.

1 Comment

That seems to be the ticket! There's no way for Spring to send back a header, if you're only sending back a payload, instead of an entire ResponseEntity. Thanks!
0

You have to set which custom headers will be exposed on response headers, on a list. Like:

 List<String> customHeaders = new ArrayList<String>();
    customHeaders.add("<Custom header name>");
    headers.setAccessControlExposeHeaders(customHeaders);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.