0

I have a code where I POST a request and the response code is 201 created with "Location": "URL" header of the new created resource.

final Response response = await _http.post(getUri(_url), body: plan);
return _getPlan(response.headers['location']);

Where _http is a BrowserClient from the package http-0.12.0+1. But the response.headers['Location'] is empty. The headers array itself contains only one element Content-Type. In Chrome developers tools I see all response headers including Location. What am I doing wrong? How can I access the response headers?

Martin Edlman
  • 665
  • 7
  • 15

2 Answers2

2

I assume the response misses the header

Access-Control-Allow-Headers:location

and therefore the browser prevents JavaScript (Dart) accessing the header (CORS).

To make the header available to JavaScript (Dart) you need to change the server to include above header in responses.

See also https://stackoverflow.com/a/36281935/217408

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    Thank you for a hint. Your answer directed me to the right header. Correct header to be added is ``Access-Control-Expose-Headers`` (not ``Access-Control-Allow-Headers``). See my answer bellow. – Martin Edlman Apr 02 '19 at 12:01
1

Thanks to Günter Zöchbauer for pointing me for the answer. As described at https://stackoverflow.com/a/39021128/1444083 the missing header is Access-Control-Expose-Headers (not Access-Control-Allow-Headers).

As I use Dart Aqueduct on the server side I had to add this line to the Channel prepare() method

class MyAppChannel extends ApplicationChannel {
  @override
  Future prepare() async {
    // ... some code
    CORSPolicy.defaultPolicy.exposedResponseHeaders.add('location');
  }
}

so the responses now contain Access-Control-Expose-Headers: location

Martin Edlman
  • 665
  • 7
  • 15