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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With